[英]Elasticsearch: conditionally sort on 2 fields, 1 replaces the other if it exists
Without scripting, I need to sort records based on rating. 没有脚本,我需要根据评级对记录进行排序。 The system-rating exists for all records, but a user-rating may or may not exist.
所有记录都存在系统评级,但用户评级可能存在也可能不存在。 If a user-rating does exist I want to use that value in the sort instead of the system-rating, for that particular record and only for that record.
如果用户评级确实存在,我想在排序中使用该值而不是系统评级,对于该特定记录而言仅用于该记录。
Tried looking into the missing
setting but it only allows _first
, _last
or a custom value (that will be used for missing docs as the sort value): 尝试查看
missing
设置,但它只允许_first
, _last
或自定义值(将用于缺少文档作为排序值):
{
"sort" : [
{ "user_rating" : {"missing" : "_last"} },
],
"query" : {
"term" : { "meal" : "cabbage" }
}
}
...but is there a way to specify the custom value should be system_rating
when user_rating
is missing? ...但有没有办法在
user_rating
缺失时指定自定义值应该是system_rating
?
I can do the following: 我可以做以下事情:
query_hash[:sort] = []
if user_rating.exist?
query_hash[:sort] << {
"user_rating" => {
"order": sort_direction,
"unmapped_type": "long",
"missing": "_last",
}
}
end
query_hash[:sort] << {
"system_rating" => {
"order": sort_direction,
"unmapped_type": "long",
}
}
...but that will always sort user rated records on top regardless of the user_rating
value. ...但无论
user_rating
值如何,它总是会将用户评分的记录排在user_rating
。
I know that scripting will allow me to do it but we cannot use scripting. 我知道脚本将允许我这样做,但我们不能使用脚本。 Is it possible?
可能吗?
唯一的方法是在索引时编写脚本或构建自定义字段,该字段将包含已经构建的排序值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.