[英]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.