我在我的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
我很确定我的索引设置错误,但在弹性搜索模型测试中搜索了一段时间后,我找不到任何相关内容。 有人可以帮我解决这个问题吗?提前谢谢。
答案 0 :(得分:0)
问题是名称的类型是文本。 从弹性搜索5,您无法在默认情况下对分析的字段进行排序
可以在具有field_data或启用doc_values的字段上进行排序 - > elasticsearch用于排序和聚合的数据结构。
你可以做两件事
https://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/doc-values.html
答案 1 :(得分:0)
您可以使用多字段:
在您的 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
看看这些链接: