# Filter terms aggregation on array field

**URL:** <https://discuss.elastic.co/t/filter-terms-aggregation-on-array-field/296627>\
**Category:** Elasticsearch\
**Created:** [February 8, 2022, 3:39pm UTC](https://discuss.elastic.co/t/filter-terms-aggregation-on-array-field/296627 "2022-02-08T15:39:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![silarhi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/silarhi/32/101538_2.png) [@silarhi](https://discuss.elastic.co/u/silarhi)\
**Post date:** [February 8, 2022, 3:39pm UTC](https://discuss.elastic.co/t/filter-terms-aggregation-on-array-field/296627/1 "2022-02-08T15:39:45Z")

</div>

Hello there,

I'm looking for a solution to filter some aggregation terms based on an `array` field.

Here's an example of data:

```auto
PUT /test-index

POST /test-index/_doc/
{
  "type": "album",
  "authors": [1, 2, 3, 4, 5]
}

POST /test-index/_doc/
{
  "type": "album",
  "authors": [1, 2]
}

POST /test-index/_doc/
{
  "type": "vinyle",
  "authors": [3]
}

```

And the tried solutions:  
**Query filter not working**

```auto
GET test-index/_search
{
  "query": {
    "bool": {
      "should": [],
      "must": [],
      "filter": [
        {
          "terms": {
            "authors": [
              1,
              2
            ]
          }
        }
      ]
    }
  },
  "size": 0,
  "aggs": {
    "authors_type": {
      "terms": {
        "size": 10,
        "field": "authors"
      }
    }
  }
}

```

**Aggregation filter not working either**

```auto
GET test-index/_search
{
  "size": 0,
  "aggs": {
    "author_types": {
      "filter": {
        "terms": {
          "authors": [
            1,
            2
          ]
        }
      },
      "aggs": {
        "foo2": {
          "filter": {
            "terms": {
              "authors": [
                1,
                2
              ]
            }
          },
          "aggs": {
            "foo3": {
              "terms": {
                "field": "authors"
              }
            }
          }
        }
      }
    }
  }
}

```

Both responses includes data with other authors than 1 & 2 as mentioned.  
Someone faced this issue too?

Should I have to update the index to use the `nested` + `inner_hits` combo?

Thanks,  
Guillaume

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [February 9, 2022, 9:29am UTC](https://discuss.elastic.co/t/filter-terms-aggregation-on-array-field/296627/2 "2022-02-09T09:29:16Z")

</div>

To rule out other authors than 1&2, you have to list up all the authors in `"must not"` clause in boolean query. There is no negation syntax for term level.

Discussion about ["How to find docs that contain the eact specified terms"](https://discuss.elastic.co/t/how-to-find-docs-that-contain-the-exact-specified-terms/295758) looks related to your purpose. Hopefully it may help you.

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [February 9, 2022, 9:38am UTC](https://discuss.elastic.co/t/filter-terms-aggregation-on-array-field/296627/3 "2022-02-09T09:38:21Z")

</div>

I was not correct. I found a solution.

```auto
GET /test-index/_search
{
  "query":{
    "bool": {
      "filter":[{
        "terms": {
          "authors": [
            "1",
            "2"
          ]
        }
      }],
      "must_not": [
        {
          "regexp": {
            "authors": "~(1|2)"
          }
        }
      ]
    }
  }
}

```

> <https://stackoverflow.com/questions/38645755/negative-lookahead-regex-on-elasticsearch/38665819#38665819>

---

<div class="post-metadata">

**Author:** ![silarhi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/silarhi/32/101538_2.png) [@silarhi](https://discuss.elastic.co/u/silarhi)\
**Post date:** [February 9, 2022, 4:36pm UTC](https://discuss.elastic.co/t/filter-terms-aggregation-on-array-field/296627/4 "2022-02-09T16:36:27Z")

</div>

Hello Tomo,

Thanks for your time and your reply.  
Unfortunately, the `must_not` filter does not work on long fields!

I've discovered the `include` key to filter my terms, will investigate in that way too.

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [February 9, 2022, 4:56pm UTC](https://discuss.elastic.co/t/filter-terms-aggregation-on-array-field/296627/5 "2022-02-09T16:56:06Z")

</div>

> [@silarhi](#):
>
> ```auto
> {
> "type": "album",
> "authors": [1, 2, 3, 4, 5]
> }
> 
> ```

I misunderstood you want to rule out such album containing authors other than 1&2.

To filtering the output of the terms aggregation, `include` might be a proper way.

---

<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 9, 2022, 4:56pm UTC](https://discuss.elastic.co/t/filter-terms-aggregation-on-array-field/296627/6 "2022-03-09T16:56:10Z")

</div>

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