I tried to process my data as below:
- group documents by its
country
field withterms
aggregation. - in each bucket, I select the document with the highest
price
value by usingtop_hits
aggregation.
Now, I want to sort aggregated buckets by the vendor_name
of the selected documents of each bucket. I tried with this query
{
"size": 0,
"query": {
"match_all": {}
},
"aggs": {
"main_aggs": {
"terms": {
"field": "country",
"size": 1000000000
},
"aggs": {
"docs_aggs": {
"top_hits": {
"size": 1,
"_source": true,
"sort": [
{
"price": {
"order": "desc"
}
}
]
}
},
"main_bucket_sort": {
"bucket_sort": {
"sort": [
{
"docs_aggs.hits.vendor_name": "desc"
}
],
"size": 5
}
}
}
}
}
}
However, Elasticsearch throws and error telling that:
No aggregation found for path [docs_aggs.hits.vendor]
My question is how we can sort aggregated buckets based on the output of the top_hits
aggregation?
I really appreciate any help you can provide.