# Writing aggregate with filtering

**URL:** <https://discuss.elastic.co/t/writing-aggregate-with-filtering/200930>\
**Category:** Elasticsearch\
**Created:** [September 24, 2019, 8:15pm UTC](https://discuss.elastic.co/t/writing-aggregate-with-filtering/200930 "2019-09-24T20:15:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dennis\_Monsewicz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dennis_monsewicz/32/53037_2.png) [@Dennis\_Monsewicz](https://discuss.elastic.co/u/Dennis_Monsewicz)\
**Post date:** [September 24, 2019, 8:15pm UTC](https://discuss.elastic.co/t/writing-aggregate-with-filtering/200930/1 "2019-09-24T20:15:39Z")

</div>

I'm trying to write an ElasticSearch aggregate query that includes filtering where I am wanting to grab all of the tags off a all of the documents where a user id matches the one I specify.

I have this code:

```
{
  "size": 0,
  "aggs": {
    "tags": {
      "filter": { "term": { "account_id": 123 } },
      "terms": {
        "field": "tag_list",
        "size": 10
      }
    }
  }
}

```

But I get the following error:

```auto
      {
        "type": "parsing_exception",
        "reason": "Found two aggregation type definitions in [tags]: [filter] and [terms]",
        "line": 6,
        "col": 16
      }

```

The document mapping contains a `tag_list` attribute that is an array of tags.

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [September 25, 2019, 6:31am UTC](https://discuss.elastic.co/t/writing-aggregate-with-filtering/200930/2 "2019-09-25T06:31:32Z")

</div>

Your request is malformed. You need to nest the terms aggregation inside the filter aggregation like this:

```auto
{
  "size": 0,
  "aggs": {
    "my_filter": {
      "filter": {
        "term": {
          "account_id": 43049
        }
      },
      "aggs": {
        "tags": {
          "terms": {
            "field": "tag_list",
            "size": 10
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Dennis\_Monsewicz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dennis_monsewicz/32/53037_2.png) [@Dennis\_Monsewicz](https://discuss.elastic.co/u/Dennis_Monsewicz)\
**Post date:** [October 1, 2019, 10:53pm UTC](https://discuss.elastic.co/t/writing-aggregate-with-filtering/200930/3 "2019-10-01T22:53:57Z")

</div>

Thank you!! I think this is working, but for some reason its not returning all of the collected tags across all documents, even when I remove the `size: 10`

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [October 2, 2019, 5:30am UTC](https://discuss.elastic.co/t/writing-aggregate-with-filtering/200930/4 "2019-10-02T05:30:55Z")

</div>

`10` is the default value of `size`, so removing it does not make a difference. Try setting it to `100` or even `1000`.

If you have a very large number of tags, you could also consider wrapping your terms aggregation in a [composite aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html). The composite agg allows you to paginate through all the buckets.

---

<div class="post-metadata">

**Author:** ![Dennis\_Monsewicz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dennis_monsewicz/32/53037_2.png) [@Dennis\_Monsewicz](https://discuss.elastic.co/u/Dennis_Monsewicz)\
**Post date:** [October 2, 2019, 1:04pm UTC](https://discuss.elastic.co/t/writing-aggregate-with-filtering/200930/5 "2019-10-02T13:04:06Z")

</div>

Thanks @abdon!! I actually figured out my question shortly after asking. I'll look into the composite aggregation. For now, I'll set the size to 1000

---

<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:** [October 30, 2019, 1:04pm UTC](https://discuss.elastic.co/t/writing-aggregate-with-filtering/200930/6 "2019-10-30T13:04:08Z")

</div>

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