[英]ElasticSearch get top 2 results per specific term in aggregations
鉴于此查询,我只想检索并限制每个国家/地区的前 2 个城市。 因此,给定最受欢迎的国家,检索前 2 个城市,然后是下一个国家、前 2 个城市等等。
{
"size": 0,
"aggs": {
"user_city_id": {
"terms": {
"field": "user.city_id",
"size": 999
},
"aggs": {
"user_country_id": {
"terms": {
"field": "user.country_id",
"size": 1
},
"aggs": {
"user_name": {
"terms": {
"field": "user.name",
"size": 1
}
}
}
}
}
}
},
"query": {
"bool": {
"must": [
{
"term": {
"user.category": 1
}
}
]
}
}
}
下面的聚合查询为每个国家/地区提供 2 个城市。
{
"aggs": {
"user_country_id": {
"terms": {
"field": "user.country_id",
"size": 10
},
"aggs": {
"user_city_id": {
"terms": {
"field": "user.city_id",
"size": 2
}
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.