Home > Software engineering >  Multiple term filters for the same field in ElasticSearch query
Multiple term filters for the same field in ElasticSearch query

Time:08-24

I'm trying to filter my full-text search to all documents that have both tag.id = 3 AND tag.id = 9.

So far I have this approach:

{
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "tags.id": "3"
                }
              },
              {
                "term": {
                  "tags.id": "9"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

That seems to work just the same as this one:

{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "tags.id": "3"
          }
        },
        {
          "term": {
            "tags.id": "9"
          }
        }
      ]
    }
  }
}

Is there any difference between both alternatives?

CodePudding user response:

Tldr;

As both work the same, the only difference is the complexity of the expression.

To keep it simple I would advice to use the later one.

  • Related