# Bucketed aggregation on nested fields

**URL:** <https://discuss.elastic.co/t/bucketed-aggregation-on-nested-fields/254729>\
**Category:** Elasticsearch\
**Created:** [November 9, 2020, 10:17am UTC](https://discuss.elastic.co/t/bucketed-aggregation-on-nested-fields/254729 "2020-11-09T10:17:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matteo\_Grolla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matteo_grolla/32/48451_2.png) [@Matteo\_Grolla](https://discuss.elastic.co/u/Matteo_Grolla)\
**Post date:** [November 9, 2020, 10:17am UTC](https://discuss.elastic.co/t/bucketed-aggregation-on-nested-fields/254729/1 "2020-11-09T10:17:28Z")

</div>

Good morning,  
I need to compute a bucketed aggregation on nested fields where the buckets correspond to the nested objects.  
Can you tell me if it’s possible at all and in case help me with the query?  
For clarity I refer to this example from ES documentation

> **[Nested aggregation | Elasticsearch Reference \[7.9\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-nested-aggregation.html)**

The docs are resellers (resellers are the nested entities) prices for products.

```auto
{
  "name": "LED TV", 
  "resellers": [
    {
      "reseller": "companyA",
      "price": 350
    },
    {
      "reseller": "companyB",
      "price": 500
    }
  ]
}

{
  "name": "Washing Machine", 
  "resellers": [
    {
      "reseller": "companyC",
      "price": 1350
    },
    {
      "reseller": "companyD",
      "price": 1500
    }
  ]
}

```

The query from ES documentation computes the minimum price for a specific product  
Query

```auto
{
  "query": {
    "match": { "name": "led tv" }
  },
  "aggs": {
    "resellers": {
      "nested": {
        "path": "resellers"
      },
      "aggs": {
        "min_price": { "min": { "field": "resellers.price" } }
      }
    }
  }
}

```

Response

```auto
{
  ...
  "aggregations": {
       "min_price": {
           "value": 350.0
       }
}

```

What we are looking for is a query to compute the minimum price for each product  
Query: ?

Expected Response:

```auto
{
  ...
  "aggregations": {
    "resellers": {
      "min_prices": [
       {“name”:“Led TV”, "value": 350},
       {“name”:“Washing Machine”, "value": 1350}
      ]
    }
  }
}

```

Thank you

---

<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:** [December 7, 2020, 10:17am UTC](https://discuss.elastic.co/t/bucketed-aggregation-on-nested-fields/254729/2 "2020-12-07T10:17:30Z")

</div>

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