[英]Order results from query by custom score defined in aggregation
I have a bunch of objects like this:我有一堆这样的对象:
{
"id": "12dfb835-6a0c-4047-ab51-d4e34116478e"
"foo": "bar"
}
{
"id": "3297b9d2-e05c-4874-91e4-c5b11e8e38c2"
"foo": "bar"
}
I also have the below query.我也有以下查询。 It basically groups documents by id
, then calculates a score based on a script.它基本上按id
对文档进行分组,然后根据脚本计算分数。
{
"query": {
"bool": {
"must": [
{
"match_all": {}
}
]
}
},
"aggs": {
"userID": {
"terms": {
"field": "id"
},
"aggs": {
"my-awesome-score": {
"scripted_metric": {
"init_script": "state.scores = []",
"map_script": "state.scores.add(42)",
"combine_script": "double score = 0; for (s in state.scores) { score += s } return score",
"reduce_script": "double score = 0; for (a in states) { score += a } return score"
}
}
}
}
}
}
This returns results like this:这将返回如下结果:
"buckets" : [
{
"key" : "12dfb835-6a0c-4047-ab51-d4e34116478e",
"doc_count" : 1,
"my-awesome-score" : {
"value" : 42.0
}
},
{
"key" : "3297b9d2-e05c-4874-91e4-c5b11e8e38c2",
"doc_count" : 1,
"my-awesome-score" : {
"value" : 42.0
}
}
]
However, in the hits
of my query result, all my documents have the default _score
of 1. How do I get Elastic to use my-awesome-score
as the value for _score
for my hits, and sort by that metric?但是,在我的查询结果中,我的所有文档的默认_score
为 1。如何让 Elastic 使用my-awesome-score
作为我的hits
的_score
值,并按该指标排序?
The aggregations API is separated from the hits/search API so aggregation result cannot be used to influence scores or sorts returned along with the hits.聚合 API 与命中/搜索 API 分开,因此聚合结果不能用于影响与命中一起返回的分数或排序。
You could apply script-based sorting but the main takeaway here is that the score is confined to a single hit and unlike a scripted metric aggregation mentioned in your snippet, it'll never have access to other docs' attributes.您可以应用基于脚本的排序,但这里的主要内容是分数仅限于一次点击,并且与您的片段中提到的脚本化指标聚合不同,它永远无法访问其他文档的属性。 In other words, your script can generate a score just for the doc it's currently passing through -- independently of the other docs.换句话说,您的脚本可以仅为它当前通过的文档生成分数——独立于其他文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.