Combine 'should' with 'filter' (search API)

When combining should with filter, the conditions in should are ignored. When using must, the filter works as expected.

Query without filter:

    {
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "address": "mill"
              }
            },
            {
              "match": {
                "address": "lane"
              }
            }
          ]
        }
      }
    }

Hits: 19

Example including filter:

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "address": "mill"
          }
        },
        {
          "match": {
            "address": "lane"
          }
        }
      ],
      "filter": {
        "range": {
          "balance": {
            "gte": 20000,
            "lte": 30000
          }
        }
      }
    }
  }
}

Hits: 217 (basically ignoring the should condition)

Thanks for your help,
Christian

1 Like

The should clause of the bool query is for optional search criteria. Let's say you are looking for hotels and would prefer for the hotel to have a swimming pool, but you would also like to see hotels without a swimming pool in the search results, then the query for swimming pool would be in the should clause. Elasticsearch will give the documents that match more should queries a higher score, so those documents will be ranked higher in the search results.

When you only have a should clause (as in your first query), then at least one of the should clauses must match for a document to be considered a hit.

When you combine a should clause with a filter than all should clauses are optional, so even documents that only match the filter will be returned. You can change that behavior by setting minimum_should_match on the bool query. You could set that for example to 1 to make it mandatory for at least one should clause to match. The docs have an example of that: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html

7 Likes

Great @abdon, Thanks a lot!

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