# Muliple nested queries and aggregations

**URL:** https://discuss.elastic.co/t/muliple-nested-queries-and-aggregations/50412
**Category:** Elasticsearch
**Created:** [May 19, 2016, 8:46am UTC](https://discuss.elastic.co/t/muliple-nested-queries-and-aggregations/50412 "2016-05-19T08:46:55Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![ffrinton](https://avatars.discourse-cdn.com/v4/letter/f/e0b2c6/32.png) [@ffrinton](https://discuss.elastic.co/u/ffrinton)
#### Post date: [May 19, 2016, 8:46am UTC](https://discuss.elastic.co/t/muliple-nested-queries-and-aggregations/50412/1 "2016-05-19T08:46:55Z")

</div>

Hi,

ES 2.1.1

[This post](http://stackoverflow.com/questions/32415031/aggregation-on-filtered-nested-inner-hits-query-in-elasticsearch) explains that when a nested sub-query is used in conjunction with aggregation on nested fields, that the sub-query needs to be repeated as a `filter` in the aggregation.

The documentation also stated that it is possible to have multiple nested sub-queries.

I have a case where a query contains multiple nested sub-queries (on the same nested object) within an overall query including non-nested fields. In boolean terms it looks as follows. History objects are the only nested objects in the mapping.

```
AND
    severity = urgent
    OR
        AND
            company.name = abc
            NESTED
                AND
                    history.created_at ≥ 2016-01-01
                    history.action_name = opened
        NESTED
            AND
                history.created_at ≥ 2015-06-01
                history.action_name = closed

```

The equivalent elastic query is [here](http://paste.ubuntu.com/16504741/)

My question is how to handle the multiple nested sub-queries in the aggregation clause if I want to aggregate on history.action\_name?

---

<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:50pm UTC](https://discuss.elastic.co/t/muliple-nested-queries-and-aggregations/50412/2 "2017-07-05T22:50:28Z")

</div>


