如何在 Elasticsearch 中使用按国家/地区划分的最大聚合来获取最大值和 id



按国家/地区获取最大值,但我想要最大值 id 的其他信息。我尝试了很多方法,但我不知道如何获取。

{

"aggs" : {
"country_groups" : {
"terms" : { "field" : "country.keyword",
"size":30000
},
"aggs":{
"max_price":{
"max": { "field" : "video_count"}
}
}

} }

}

根据id字段的类型(数字或字符串(,有两种方法可以做到这一点。

如果您查看下面的查询,如果您的id是数字,您可以执行与video_count相同的操作,即使用max指标聚合(请参阅max_id_num(。

但是,如果id字段是字符串,则可以利用top_hits聚合并按descending顺序对其进行排序(请参阅max_id_str(。

{
"aggs": {
"country_groups": {
"terms": {
"field": "country.keyword",
"size": 30000
},
"aggs": {
"max_price_and_id": {
"top_hits": {
"size": 1,
"sort": {
"video_count": "desc"
},
"_source": ["channel_id", "video_count"]
}
}
}
}
}
}

最新更新