# Return all documents that have the highest value in a field

**URL:** <https://discuss.elastic.co/t/return-all-documents-that-have-the-highest-value-in-a-field/121138>\
**Category:** Elasticsearch\
**Created:** [February 22, 2018, 10:33pm UTC](https://discuss.elastic.co/t/return-all-documents-that-have-the-highest-value-in-a-field/121138 "2018-02-22T22:33:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vioh](https://avatars.discourse-cdn.com/v4/letter/v/b4bc9f/32.png) [@Vioh](https://discuss.elastic.co/u/Vioh)\
**Post date:** [February 22, 2018, 10:33pm UTC](https://discuss.elastic.co/t/return-all-documents-that-have-the-highest-value-in-a-field/121138/1 "2018-02-22T22:33:33Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 22, 2018, 10:33pm UTC](https://discuss.elastic.co/t/return-all-documents-that-have-the-highest-value-in-a-field/121138/2 "2018-03-22T22:33:35Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
