【问题标题】:Don't know how to sort elasticsearch-model results不知道如何对 elasticsearch-model 结果进行排序
【发布时间】:2018-07-09 06:00:00
【问题描述】:

我在我的 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 测试中搜索了一段时间后,我找不到任何相关的东西。 有人可以帮我解决这个问题吗?提前致谢。

【问题讨论】:

    标签: ruby-on-rails ruby elasticsearch elasticsearch-model


    【解决方案1】:

    当然问题是名称的类型是文本。 在弹性搜索 5 中,默认情况下您无法对分析的字段进行排序

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

    • 无法在分析的字符串字段上启用 Doc_values。
    • 默认情况下,在分析的字符串字段上禁用 field_data。

    你可以做两件事

    • 要么更改名称到关键字的映射->这将是 non_analyzed,然后将在其上启用 doc_values。
    • 或者您可以使用 "fielddata": true 在字段 name 上启用 field_data 这里是引用这个的链接

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

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

    【讨论】:

    • @ngw 是否通过添加 indexes :name, type: 'text', fielddate: true 将其转换为 ruby​​?奇怪的是,我似乎只在我的 CI 中而不是在本地环境中遇到错误:s 编辑:不,这会导致崩溃,你在哪里添加 fielddata 在 ruby​​ 中?
    【解决方案2】:

    您可以使用多字段:

    在你的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
    

    看看这些链接:

    【讨论】:

      猜你喜欢
      • 2021-10-21
      • 2016-04-02
      • 2011-06-29
      • 1970-01-01
      • 2019-04-02
      • 2016-05-21
      • 2017-11-12
      • 1970-01-01
      • 2019-07-12
      相关资源
      最近更新 更多