# Get the corresponding document from a min aggregation

**URL:** <https://discuss.elastic.co/t/get-the-corresponding-document-from-a-min-aggregation/184156>\
**Category:** Elasticsearch\
**Created:** [June 4, 2019, 10:50am UTC](https://discuss.elastic.co/t/get-the-corresponding-document-from-a-min-aggregation/184156 "2019-06-04T10:50:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Toast](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/toast/32/47445_2.png) [@Toast](https://discuss.elastic.co/u/Toast)\
**Post date:** [June 4, 2019, 10:50am UTC](https://discuss.elastic.co/t/get-the-corresponding-document-from-a-min-aggregation/184156/1 "2019-06-04T10:50:23Z")

</div>

Hello,

i'm querying an aggregation for the minimum and maximum price in an index, like this:

```
"aggregations": {
    "PriceMin": {
      "min": {"field": "PriceProduct"}
    },
    "PriceMax": {
      "max": {"field": "PriceProduct"}
    }
  }

```

However, i'd like to get the corresponding documents, i.e. the document that contains the min price, and also the one that contains the max price. If several documents apply it doesn't matter if i get all of them or just a single one.

Is there a good solution for that?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [June 5, 2019, 8:20am UTC](https://discuss.elastic.co/t/get-the-corresponding-document-from-a-min-aggregation/184156/2 "2019-06-05T08:20:44Z")

</div>

Hi Toast.  
Use the `top_hits` aggregation with a `"size":1` setting and appropriate sort order.

---

<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:** [July 3, 2019, 8:20am UTC](https://discuss.elastic.co/t/get-the-corresponding-document-from-a-min-aggregation/184156/3 "2019-07-03T08:20:45Z")

</div>

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