Reputation: 195
I have an index with people. I want to retrieve a total of 100 people (according to a number of filters) but in the end want to impose a max of 3 people per location.
e.g.
Index:
| name | location |
|---------|----------|
| Frodo | Shire |
| Sam | Shire |
| Merry | Shire |
| Pippin | Shire |
| Boromir | Gondor |
| Faramir | Gondor |
The result of the query should be something like: Frodo, Sam, Merry, Boromir, Faramir
Upvotes: 0
Views: 80
Reputation: 514
I don't think it's possible to get exactly what you want. You're essentially doing a group-by (terms aggregation) on the location field. Since there's no way to know exactly how many locations you have, the number of groups is unbounded. So, if there are 101 groups and you return one name per group, you'd have 101 results. And if we're filtering down to 100 max results, which group do we drop? Random?
That said, you can get the top x locations where locations are sorted by a descending count, though you can change the sorting order. 33 location buckets w/ 3 results per bucket is 99...close enough?
So, you can get a max of 3 results per location for 33 locations using a terms aggregation and a nested top hits aggregation
GET lotr/_search?size=0
{
"aggs": {
"location": {
"terms": {
"field": "location",
"size": 33
},
"aggs": {
"name": {
"top_hits": {
"size": 3,
"_source": {
"includes": ["name"]
}
}
}
}
}
}
}
Upvotes: 2