# Query - Get min/max of nested documents field for every root document

**URL:** <https://discuss.elastic.co/t/query-get-min-max-of-nested-documents-field-for-every-root-document/31658>\
**Category:** Elasticsearch\
**Created:** [October 5, 2015, 7:27pm UTC](https://discuss.elastic.co/t/query-get-min-max-of-nested-documents-field-for-every-root-document/31658 "2015-10-05T19:27:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sumit\_jain](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sumit_jain/32/5125_2.png) [@sumit\_jain](https://discuss.elastic.co/u/sumit_jain)\
**Post date:** [October 5, 2015, 7:27pm UTC](https://discuss.elastic.co/t/query-get-min-max-of-nested-documents-field-for-every-root-document/31658/1 "2015-10-05T19:27:03Z")

</div>

I have an index of products, with price on every date of year as a nested doc.

{ name: "nexus7",  
prices: [  
{date: "2014-09-01", price: 100},  
{date: "2014-09-02", price: 200},  
...  
]}

I want to get the lowest price of each product. I tried nested aggregation

```
{
      "aggs": {
        "prices": {
          "aggs": {
            "min_price": {
              "min": {
                "field": "prices.price"
              }
            }
          }, 
          "nested": {
            "path": "prices"
          }
        }
      }
  }
}

```

But this return the minimum price among all products, which is not exactly what I need.  
Is this possible to do?

---

<div class="post-metadata">

**Author:** ![Anushank\_Lal](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@Anushank\_Lal](https://discuss.elastic.co/u/Anushank_Lal)\
**Post date:** [April 18, 2016, 7:29am UTC](https://discuss.elastic.co/t/query-get-min-max-of-nested-documents-field-for-every-root-document/31658/2 "2016-04-18T07:29:43Z")

</div>

Hi Sumit,

Had you reslove this problem? I am going through the same senairo. Please help

Thanks  
Anushank

---

<div class="post-metadata">

**Author:** ![sumit\_jain](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sumit_jain/32/5125_2.png) [@sumit\_jain](https://discuss.elastic.co/u/sumit_jain)\
**Post date:** [April 18, 2016, 7:49am UTC](https://discuss.elastic.co/t/query-get-min-max-of-nested-documents-field-for-every-root-document/31658/3 "2016-04-18T07:49:43Z")

</div>

I think it can be solved by using a terms aggregation followed by a min sub aggregation.

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [April 18, 2016, 9:23am UTC](https://discuss.elastic.co/t/query-get-min-max-of-nested-documents-field-for-every-root-document/31658/4 "2016-04-18T09:23:46Z")

</div>

Something like this should work.

```auto
{
      "aggs": {
        "products": {
          "terms": {
            "field": "name"
          },
          "aggs": {
            "prices": {
              "nested": {
                "path": "prices"
              },
              "aggs": {
                "min_price": {
                  "min": {
                    "field": "prices.price"
                  }
                }
              }
            }
          }
        }

```

---

<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 5, 2017, 10:58pm UTC](https://discuss.elastic.co/t/query-get-min-max-of-nested-documents-field-for-every-root-document/31658/5 "2017-07-05T22:58:34Z")

</div>


