ruby-on-rails - 默认情况下,在文本字段上禁用 Fielddata。设置字段数据=真

标签 ruby-on-rails elasticsearch searchkick

完整的错误信息如下:

Searchkick::InvalidQueryError: Searchkick::InvalidQueryError: [400] {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Fielddata is disabled on text fields by default. Set fielddata=true on [foo_name] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"foos_test","node":"k0yYkVnIQzaXbvpAG_rKgw","reason":{"type":"illegal_argument_exception","reason":"Fielddata is disabled on text fields by default. Set fielddata=true on [foo_name] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."}}],"caused_by":{"type":"illegal_argument_exception","reason":"Fielddata is disabled on text fields by default. Set fielddata=true on [foo_name] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."}},"status":400}

当我在索引操作中添加以下代码时出现此错误(此错误仅在 minitest 中显示):

order: {foo_name: :asc}

这是我的完整索引代码:

def index
  query = params[:q].presence || "*"
  @foos = Foo.search(
    query,
    page: params[:page], per_page: 25,
    order: {foo_name: :asc}
    )
  end
end

我正在使用 Searchkick。

最佳答案

这是一个令人讨厌的;我们很幸运偶然发现了解决方案。我假设您的模型看起来像这样:

class Foo
  attr_accessor :foo_name

  searchkick # some settings

  def search_data
    { foo_name: foo_name }
  end
end

您需要做的是定义一个 foo_name_sort 字段,其值与 foo_name 相同,但设置为可过滤:

class Foo
  attr_accessor :foo_name

  searchkick # some settings,
             filterable [:foo_name_sort]

  def search_data
    { foo_name: foo_name,
      foo_name_sort: foo_name }
  end
end

然后搜索:

@foos = Foo.search(
  query,
  page: params[:page], per_page: 25,
  order: {foo_name_sort: :asc}
  )

关于ruby-on-rails - 默认情况下,在文本字段上禁用 Fielddata。设置字段数据=真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42151110/

相关文章:

ruby-on-rails - 唯一索引和标准索引是分开做的吗?

elasticsearch - Elasticsearch query_string-确切的短语问题

elasticsearch - Elasticsearch-分组总和

ruby-on-rails - 搜索踢 rails 5

ruby - 如何将嵌套的哈希参数传递给searchkick

ruby-on-rails - searchkick 索引相关模型字段

ruby-on-rails - WebSockets : Can connect but cannot subscribe to channel

ruby-on-rails - Cucumber + Webrat + Selenium 指南

ruby-on-rails - 如何在 Rails Engine 4.1.5 中安装 Foundation

elasticsearch - Kafka 连接到 ElasticSearch 是否可以进行更新