# Aggregration Filter doesnt seem to work

**URL:** <https://discuss.elastic.co/t/aggregration-filter-doesnt-seem-to-work/275182>\
**Category:** Elasticsearch\
**Created:** [June 7, 2021, 5:15pm UTC](https://discuss.elastic.co/t/aggregration-filter-doesnt-seem-to-work/275182 "2021-06-07T17:15:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kapso](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kapso/32/89472_2.png) [@kapso](https://discuss.elastic.co/u/kapso)\
**Post date:** [June 7, 2021, 5:15pm UTC](https://discuss.elastic.co/t/aggregration-filter-doesnt-seem-to-work/275182/1 "2021-06-07T17:15:14Z")

</div>

I have document of type blog posts, here's what an example looks like

**Input:**

```
[
  {
    "id": 1,
    "post_title": "Title 1",
    "published_on": "2021-06-07",
    "tag_ids": [1, 2],
    "authors": [
      {
        "id": 12,
        "name": "Jeff Dawn",
        "emp_type": 10, # Contractor
        "country": "US"
      },
      {
        "id": 14,
        "name": "Steve",
        "emp_type": 20 # Employee ,
        "country": "CA"
      }
    ]
  },
  {
    "id": 2,
    "post_title": "Title 2",
    "published_on": "2021-06-07",
    "tag_ids": [3, 4],
    "authors": [
      {
        "id": 22,
        "name": "Ross",
        "emp_type": 20, # Employee
        "country": "US"
      }
    ]
  }
]

```

I am doing an aggregation on author id, to query the number of posts published by authors in the last 5 years, filtered by a tag id, and type of the author. So the query is...

**Query:**

```
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "published_on": {
              "gte": "2016-06-07",
              "lte": "2021-06-07"
            }
          }
        },
        {
          "terms": {
            "tag_ids": [
              9506
            ]
          }
        }
      ]
    }
  },
  "aggregations": {
    "test": {
      "filter": {
        "bool": {
          "filter": {
            "term": {
              "authors.emp_type": 10
            }
          }
        }
      },
      "aggregations": {
        "authors": {
          "terms": {
            "field": "authors.id",
            "size": 400,
            "min_doc_count": 1
          }
        }
      }
    }
  },
  "size": 0
}

```

**Problem:**

- The problem is that the aggregations are NOT being filtered on `"authors.emp_type": 10`
- I am seeing authors with `emp_type` 10 and non-10 values too
- I tried putting the `"authors.emp_type": 10` term filter inside `query/bool/filter` as well, but running into the same issue, after which I tried aggregation filter

---

<div class="post-metadata">

**Author:** ![Otteryoudoing](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/otteryoudoing/32/90198_2.png) [@Otteryoudoing](https://discuss.elastic.co/u/Otteryoudoing)\
**Post date:** [June 11, 2021, 8:58pm UTC](https://discuss.elastic.co/t/aggregration-filter-doesnt-seem-to-work/275182/2 "2021-06-11T20:58:30Z")

</div>

Hello Kapil, you can accomplish this by using 'must' in your bool query.

Documentation here - [Boolean query | Elasticsearch Guide [7.13] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html)

I have re-written your query for a test index in a cluster of mine. I believe this does what you'd like

```auto
GET testindex/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
          "authors.emp_type": 10
          }
        }
        ],
      "filter": [
        {
          "range": {
            "published_on": {
              "gte": "2016-06-07",
              "lte": "2021-06-07"
            }
          }
        },
        {
          "terms": {
            "tag_ids": [
              9506
            ]
          }
        }
      ]
    }
  },
  "aggregations": {
    "authors": {
      "terms": {
        "field": "authors.id.keyword",
        "size": 400,
        "min_doc_count": 1
      }
    }
  },
  "size": 0
}

```

Hopefully this helps!

---

<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 9, 2021, 8:59pm UTC](https://discuss.elastic.co/t/aggregration-filter-doesnt-seem-to-work/275182/3 "2021-07-09T20:59:16Z")

</div>

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