'filter' Clause

The filter clause within a bool query specifies conditions that are mandatory for a document to be considered a match, in a similar manner to 'must' clause. Unlike the must clause, the filter clause does not affect the document score.

Example -

{
  "query": {
    "bool": {
      "must": 
        { "term": { "Bird": "Asian Koel" } }
      ,
      "filter": [
        { "term": { "Country": "India" } },
        { "term": { "Color": "Black" } }
      ]
    }
  }
}

In the above example, all candidates must satisfy the condition -

  • exact match over the 'bird' field, with score equals TF-IDF score for { "Bird": "Asian Koel" }

In addition, any matched documents must satisfy the following conditions -

  • exact match over the 'Country' field, with score equals TF-IDF score for { "Bird": "Asian Koel" }

  • exact match over "Color" field, with score equals TF-IDF score for { "In Stock": "true" }

The overall score will be the rarity score, determined by the "must": { "term": { "Bird": "Asian Koel" } } clause.

Last updated