'should_not' Clause

The should_not clause within a bool query specifies conditions that are optional for a document to be considered a match, in a similar manner to 'should' clause. The should_not clause decreases the document score. The should clause is often used for expressing optional or desirable conditions.

'should_not' Clause Score

In the 'should_not' clause, each condition is associated with a designated score, and the overall score for the clause is determined by substracting these individual scores. If not explicitly specified otherwise, scoring follows the TF-IDF scoring model. The 'should_not' clause enables documents to be a lesser match if they meet any of the specified conditions.

The combined score is a subtraction of the individual scores

Score = -score1 - score2 - score3...

Example -

Copy

{
  "query": {
    "bool": {
      "must": { "Bird": "Asian Koel" }
      "should_not": [
        { "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 documents that 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" }

Will receive lower score. The overall score will be a sum of the individual scores.

Last updated