Filter terms aggregation on array field

Hello there,

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

Here's an example of data:

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

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

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

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" looks related to your purpose. Hopefully it may help you.

I was not correct. I found a solution.

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

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.

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.

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