Elasticsearch aggregation order by top hit score
20,321
I had the same issue, and the way I resolved it was to introduce a sub-aggregation on the docs score. Then in my outer aggregation, I ordered by name of the max_score aggregation.
GET /my-index/my-type/_search
{
"query": {
"bool" : {
"should" : [ {
"match" : {
"searchTerm" : {
"query" : "style",
"type" : "boolean"
}
}
},
{
"flt_field" : {
"searchTerm" : {
"like_text" : "style"
}
}
}]
}
},
"aggs": {
"group_by_target_url": {
"terms": {
"field": "targetUrl",
"order": {
"max_score": "desc"
}
},
"aggs": {
"max_score": {
"max": {
"script": "doc.score"
}
}
}
}
}
}
I followed the directions on this link:
Author by
pandora2000
Updated on January 12, 2020Comments
-
pandora2000 over 4 years
I want to order buckets by doc.score of top_hit. My current implementation is below.
group_by_iid: { terms: { field: 'iid', order: { max_score: 'desc' }, size: 0 }, aggs: { max_score: { max: { script: 'doc.score' } }, top_hit: { top_hits: { sort: [{ source_priority: { order: 'desc' } }], size: 1 } } } }
This is wrong because buckets are ordered by their top score, not their top source_priority document's score. Is there a way to solve this problem?
-
Shadocko about 9 yearsIf your documents happen to have a "score" field and you want a metrics aggregation like "max" to work with the score computed by elasticsearch rather than the document-defined one, the script in the aggregation should be "_score", not "doc.score".
-
Darby almost 9 years@Shadocko yes! that's pretty misleading. Thanks for the comment. I'm trying out the same example and now it works.
-
KingWu over 6 yearsDo you know how to solve it with partitions? My question is here stackoverflow.com/questions/46484351/…
-
rhitz about 4 years@Roy, if you can update your answer to replace doc.score to _score. It will save few minutes for people who are not reading the comments. +1