【发布时间】:2016-04-11 19:59:47
【问题描述】:
我正在尝试按 2 个字段对我的 ES 结果进行排序:可搜索和年份。
我的 Rails 应用程序中的映射:
# mapping
def as_indexed_json(options={})
as_json(only: [:id, :searchable, :year])
end
settings index: { number_of_shards: 5, number_of_replicas: 1 } do
mapping do
indexes :id, index: :not_analyzed
indexes :searchable
indexes :year
end
end
查询:
@records = Wine.search(query: {match: {searchable: {query:params[:search], fuzziness:2, prefix_length:1}}}, sort: {_score: {order: :desc}, year: {order: :desc}}, size:100)
查询中的有趣之处:
sort: {_score: {order: :desc}, year: {order: :desc}}
我认为查询与 2 个排序参数配合得很好。 我的问题是 2 个同名文档的分数不同(可搜索字段)。
例如,我正在搜索“酒厂”:
即使可搜索字段相同,您也会看到非常不同的分数。我认为问题是由于 ID 字段(实际上是 UUID)。看起来这个 ID 字段会影响分数。 但是在我的模式映射中,我写了不应该分析 ID,并且在我的 ES 查询中,我要求只在“可搜索”字段中搜索,而不是在 ID 中。
在计算相同领域的相同分数时,我错过了什么? (实际上,分数后按年份排序是没有用的,因为相同字段的分数不同)
【问题讨论】:
标签: ruby-on-rails sorting elasticsearch