Given this query, i want to retrieve and limit only to top 2 cities from each country. So, given the most popular country, retrieve top 2 cities, then next country, top 2 cities and etcera.
{
"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
}
}
]
}
}
}
CodePudding user response:
Below aggregation query give you 2 city per country.
{
"aggs": {
"user_country_id": {
"terms": {
"field": "user.country_id",
"size": 10
},
"aggs": {
"user_city_id": {
"terms": {
"field": "user.city_id",
"size": 2
}
}
}
}
}
}