Suppose that I have the following mapping:
PUT /myindex/_mappings/people
{
"properties": {
"name": {"type": "keyword"},
"age" : {"type": "integer"},
}
}
And the following documents:
{"name": "Bob", "age": 20},
{"name": "Ben", "age": 25},
{"name": "Eli", "age": 30},
{"name": "Eva", "age": 20},
{"name": "Jan", "age": 21},
{"name": "Jim", "age": 20},
{"name": "Lea", "age": 30},
How do I create a single query which returns all people that are the oldest in the index? Right now, my solution is to do 2 requests, the first one is to aggregate the max age, and then use the max age to to the second request:
GET /myindex/people/_search
{
"aggs": {
"max_age": {"max": {"field": "age"}}
}
}
GET /myindex/people/_search
{
"query": {"match": {"age": <max_age>}} // where max_age = 30 (which is the aggs result)
}
Obviously this is a really bad approach. Can anyone suggest a better solution to this? I'm using elastic javascript API 6.0 by the way. Thanks in advance!