elasticsearch - 如何在 Elasticsearch 中使用按国家/地区的 Max 聚合来获取最大值和 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"]
}
}
}
}
}
}
推荐阅读
- java - 用Java解析Json结构化文件
- biztalk - BizTalk App Insights UI 因无法从下拉列表中选择而失败
- python - 使用 python3 时出现 Lambda 函数错误
- php - 如何在我的网站上加载另一个网站的验证码?
- angularjs - 我想在角度 js 中选择列表项时显示模式窗口
- python - 当我尝试运行 tensorflow 时,我注意到 GPU 没有被使用,尽管我使用的是 tensorflow GPU 版本。有什么解决方法吗?
- kotlin - 已添加片段:GalleryFragment{ef2fd8d #6 id=0x7f090081
- javascript - 应该如何构建 HTML 的布局以便在 D3 中有多个相互关联的图表
- c++ - 检测我是在构建可执行文件还是动态库?
- json - 如何在golang中将字符串转换为json?