'should' Clause

In Hyperspace, the should clause within a bool query is used to specify conditions that are optional for a document to be considered a match. Unlike the must clause, which imposes mandatory conditions, the should clause only modifies the document score, and allows for flexibility by indicating that any of the specified conditions can be satisfied for a document to contribute to the search results. The should clause is often used for expressing optional or desirable conditions.

'should' Clause Score

Within the 'should' clause, each condition is associated with a designated score, and the overall score for the 'should' clause is determined by combining these individual scores. If not explicitly specified otherwise, scoring follows the TF-IDF scoring model. The 'should' clause is employed when you desire flexibility, as it allows for documents to be considered a match if they satisfy any of the specified conditions.

The combined score is a sum of the individual scores

Score = score1 + score2 + score3...

Example -

{
  "query": {
    "bool": {
      "must": { "Bird": "Asian Koel" }
      "should": [
        { "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 higher score. The overall score will be a sum of the individual scores.

Last updated