# How to aggregate on filtered nested documents

**URL:** <https://discuss.elastic.co/t/how-to-aggregate-on-filtered-nested-documents/45929>\
**Category:** Elasticsearch\
**Created:** [March 31, 2016, 12:00pm UTC](https://discuss.elastic.co/t/how-to-aggregate-on-filtered-nested-documents/45929 "2016-03-31T12:00:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![K\_Samanth\_Kumar\_Redd](https://avatars.discourse-cdn.com/v4/letter/k/e9c0ed/32.png) [@K\_Samanth\_Kumar\_Redd](https://discuss.elastic.co/u/K_Samanth_Kumar_Redd)\
**Post date:** [March 31, 2016, 12:00pm UTC](https://discuss.elastic.co/t/how-to-aggregate-on-filtered-nested-documents/45929/1 "2016-03-31T12:00:43Z")

</div>

Hi,

I am working aggregations. I am so happy that I am able prepare different kinds of analytical reports using aggregations.

But I am stuck while aggregating the filtered nested documents.

I have indexed customer data along with orders. Here "orders" are stored as nested type.

**_Ex:-_**  
example document:  
{"company":"ABC","orders":[{"order\_no":"OL1", "prod\_type" : "OLP",  
"price":20}, {"order\_no":"OL2", "prod\_type" : "OLP", "price":50},  
{"order\_no":"OL3", "prod\_type" : "GLP", "price":100},{"order\_no":"OL3", "prod\_type" : "OLP", "price":200} ]}

Here I want to get the total price where orders.prod\_type='OLP'  
**expected output** total\_price = 270

Can anyone please help me?

Thanks & Regards,  
Samanth

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [March 31, 2016, 1:07pm UTC](https://discuss.elastic.co/t/how-to-aggregate-on-filtered-nested-documents/45929/2 "2016-03-31T13:07:27Z")

</div>

Try the following example (it uses your example document):

```auto
POST test
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  },
  "mappings": {
    "doc": {
      "properties": {
        "orders": {
          "type": "nested"
        }
      }
    }
  }
}

GET test

POST test/doc/1
{
  "company": "ABC",
  "orders": [
    {
      "order_no": "OL1",
      "prod_type": "OLP",
      "price": 20
    },
    {
      "order_no": "OL2",
      "prod_type": "OLP",
      "price": 50
    },
    {
      "order_no": "OL3",
      "prod_type": "GLP",
      "price": 100
    },
    {
      "order_no": "OL3",
      "prod_type": "OLP",
      "price": 200
    }
  ]
}

GET test/_search
{
  "query": {
    "nested": {
      "path": "orders",
      "score_mode": "avg",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "orders.prod_type": "OLP"
              }
            }
          ]
        }
      }
    }
  },
  "aggs": {
    "orders": {
      "nested": {
        "path": "orders"
      },
      "aggs": {
        "OLP_orders": {
          "filter": {
            "match": {
              "orders.prod_type": "OLP"
            }
          },
          "aggs": {
            "total_price": {
              "sum": {
                "field": "orders.price"
              }
            }
          }
        }
      }
    }
  }
}

```

In the search request it uses the query to filter documents that have at least one order with the `prod_type` "OLP" and then in the aggregations uses the `nested` aggregation to get all of the order nested documents (the orders), uses the `filter` aggregation to filter those orders by those with a `prod_type` of "OLP" and then uses the sum aggregation to get the total price of all the "OLP" orders

Hope that helps

---

<div class="post-metadata">

**Author:** ![K\_Samanth\_Kumar\_Redd](https://avatars.discourse-cdn.com/v4/letter/k/e9c0ed/32.png) [@K\_Samanth\_Kumar\_Redd](https://discuss.elastic.co/u/K_Samanth_Kumar_Redd)\
**Post date:** [April 4, 2016, 4:33am UTC](https://discuss.elastic.co/t/how-to-aggregate-on-filtered-nested-documents/45929/3 "2016-04-04T04:33:07Z")

</div>

Wow! Thank You so much!... It is working!!!

I am so happy now.

Thanks again!

Regards,  
Samanth

---

<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, 11:02pm UTC](https://discuss.elastic.co/t/how-to-aggregate-on-filtered-nested-documents/45929/4 "2017-07-05T23:02:47Z")

</div>


