# Elasticsearch nested aggregate query with filter

**URL:** <https://discuss.elastic.co/t/elasticsearch-nested-aggregate-query-with-filter/192523>\
**Category:** Elasticsearch\
**Created:** [July 27, 2019, 12:28pm UTC](https://discuss.elastic.co/t/elasticsearch-nested-aggregate-query-with-filter/192523 "2019-07-27T12:28:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kgp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kgp/32/51037_2.png) [@kgp](https://discuss.elastic.co/u/kgp)\
**Post date:** [July 27, 2019, 12:28pm UTC](https://discuss.elastic.co/t/elasticsearch-nested-aggregate-query-with-filter/192523/1 "2019-07-27T12:28:29Z")

</div>

I'm trying to apply a filter to a nested aggregate query. I'm unable to apply the filter.

I have data like below. Nested dictionaries(each represent an address) in a list. Some of the addresses have "geo\_location" and some of them do not. I want to find out the counts of "type" of addresses(aggregate query). for ex: type 1: 45000, type 2: 40,000, but I want the count only for the addresses which do not have "geo\_location" field populated.

```auto
"addresses": [
            {
              "geo_location": {
                "lat": 42.407345,
                "lon": -83.169199
              },

              "type": 1,
              "addr_id": 26816125,

            },
            {

              "geo_source": "RESIDENTIAL",
              "geo_location": {
                "lat": 42.352785,
                "lon": -83.311244
              },
              "type": 1,

            },
              {
              "type": 2,
              "addr_id": 3374602,
            },
            {
              "type": 2,
              "addr_id": 99446474,
            }
          ]

```

I have tried to do an aggregate based on the types, but I'm unable to filter it for the addresses which do not have "geo\_location" field in it.

```auto
GET /search/_search
{
  "size": 0,
  "aggs": {
    "Nesting": {
      "nested": {
        "path": "addresses"
      },

      "aggs": {
        "keyword_names": {
          "terms": {
            "field": "addresses.type",
            "size": 100
          } }
      }
    }
  }
}

```

---

<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:** [August 24, 2019, 12:28pm UTC](https://discuss.elastic.co/t/elasticsearch-nested-aggregate-query-with-filter/192523/2 "2019-08-24T12:28:39Z")

</div>

Closed on August 24, 2019, 12:28pm UTC

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