# Elasticsearch 5 | filter nested object and nested aggregation count

**URL:** https://discuss.elastic.co/t/elasticsearch-5-filter-nested-object-and-nested-aggregation-count/74726
**Category:** Elasticsearch
**Created:** [February 10, 2017, 11:08pm UTC](https://discuss.elastic.co/t/elasticsearch-5-filter-nested-object-and-nested-aggregation-count/74726 "2017-02-10T23:08:42Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Joost](https://avatars.discourse-cdn.com/v4/letter/j/6de8d8/32.png) [@Joost](https://discuss.elastic.co/u/Joost)
#### Post date: [February 10, 2017, 11:08pm UTC](https://discuss.elastic.co/t/elasticsearch-5-filter-nested-object-and-nested-aggregation-count/74726/1 "2017-02-10T23:08:42Z")

</div>

elasticsearch 5.2

I want to filter my items by the nested object [state.id](http://state.id) = 101 and an aggregation count for all the items by [state.id](http://state.id). There are items with the state ids 101, 102 and 103.

The aggregation count is now limited to [state.id](http://state.id) 101. I want only get the items with [state.id](http://state.id) = 101 and the aggregation count for all state ids.

How can i do that?

```auto
GET index/items/_search
{
  "query": {
        "nested" : {
            "path" : "state",
            "query": {
              "bool": {
                "filter": {
                  "term": { "state.id": 101 }
                }
              }
            }
        }
  },
     "aggs" : {
        "state" : {
            "nested" : {
                "path" : "state"
            },
            "aggs" : {
                "count" : { "terms" : { "field" : "state.id" } }
            }
        }
    }
}

```

---

<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: [March 10, 2017, 11:08pm UTC](https://discuss.elastic.co/t/elasticsearch-5-filter-nested-object-and-nested-aggregation-count/74726/2 "2017-03-10T23:08:58Z")

</div>

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