繁体   English   中英

不知道如何对elasticsearch-model 结果进行排序

[英]Don't know how to sort elasticsearch-model results

我在我的 RoR 应用程序上使用 elasticsearch-model 来执行搜索并对结果进行排序。 我可以执行查询并返回未排序的结果,但是当我添加排序时,一切都会中断:

Elasticsearch::Transport::Transport::Errors::BadRequest: [400] {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Fielddata is disabled on text fields by default. Set fielddata=true on [name] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"profiles","node":"mad6gavaR3yTFabsF9m0rg","reason":{"type":"illegal_argument_exception","reason":"Fielddata is disabled on text fields by default. Set fielddata=true on [name] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."}}]},"status":400}
from /Users/ngw/.rvm/gems/ruby-2.2.2@utelier/gems/elasticsearch-transport-5.0.4/lib/elasticsearch/transport/transport/base.rb:202:in `__raise_transport_error'

这显然是在告诉我,我配置索引的方式是错误的。 这是我正在索引的内容

def as_indexed_json(options={})
  {
    profile_type:     profile_type,
    name:             name,
    specialisation:   specialisation,
    description:      description,
    tags:             tags,
    minimum_order:    minimum_order,
    company_city:     company_city,
    company_address:  company_address,
    continent_id:     country.try(:continent).try(:id),
    country_id:       country.try(:id),
    industry:         industry.try(:id)
  }
end

查询可以使用这些字段中的任何一个,但不能使用仅用于排序目的的 :name。 我的索引的配置很简单:

settings index: { number_of_shards: 1 } do
  mapping dynamic: false do
    indexes :name, type: 'text'
    indexes :description, analyzer: 'english'
  end
end

我很确定我的索引设置错误,但是在 elasticsearch-model 测试中搜索了一段时间后,我找不到任何相关的东西。 有人可以帮我解决这个问题吗? 提前致谢。

当然问题是 name 的类型是text 从弹性搜索 5 开始,默认情况下您无法对分析的字段进行排序

可以在启用 field_data 或 doc_values 的字段上进行排序 -> elasticsearch 用于排序和聚合的数据结构。

  • 无法在分析的字符串字段上启用 Doc_values。
  • 并且 field_data 在分析的字符串字段上默认是禁用的。

你可以做两件事

  • 要么将 name 的映射更改为 keyword-> 这将是 non_analyzed,然后将在其上启用 doc_values。
  • 或者,您可以使用“fielddata”在字段名称上启用 field_data :true这是引用此内容的链接

https://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata.html

https://www.elastic.co/guide/en/elasticsearch/reference/current/doc-values.html

您可以使用多字段:

在您的indexes

settings index: { number_of_shards: 1 } do
  mapping dynamic: false do
    indexes :name, type: 'text', fields: { keyword: { type: :keyword } }
    indexes :description, analyzer: 'english'
  end
end

在您的搜索中:

Model.search(
  query: ...
  sort: {
    'name.keyword':  { order: 'asc' }
  }
)

或者只是将fielddata设置为true (警告:不推荐,因为它使用了更多资源):

settings index: { number_of_shards: 1 } do
  mapping dynamic: false do
    indexes :name, type: 'text', fielddata: true
    indexes :description, analyzer: 'english'
  end
end

看看这些链接:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM