Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Controlling Search Results  ›  Lesson

bool: must and should

must 12 minIn a bool query, must clauses are required and scored, while should clauses are optional bonuses unless there is no must or filter.

bool: must and should

A shopper searches "garden" and ticks "fiction". Another just wants garden books, "and ideally ones by Mira Das near the top". One query has two required conditions; the other has one required condition and one nice-to-have. The bool query's must and should clauses express exactly that difference.

The rule

must is required and scored; should is optional and adds score.

A bool query holds lists of other queries in named clauses. This lesson covers the two that affect relevance scores:

  • must — every query listed must match. Each one also adds to the document's _score, so better matches rank higher.
  • should — queries that may match. Each one that does adds to the score, pushing the document up the list.
GET /books/_search
{
  "query": {
    "bool": {
      "must":   [ { "match": { "title": "garden" } } ],
      "should": [ { "term":  { "author": "Mira Das" } } ]
    }
  }
}

Only The Quiet Garden contains the term garden (remember: "gardening" is a different term to the standard analyzer). It matches must, and being by Mira Das, it also collects the should bonus. A garden book by any other author would still appear, just lower down.

One rule changes everything: if a bool has no must or filter clause, at least one should must match. Then should behaves like OR:

{ "query": { "bool": { "should": [
  { "term": { "genre": "fiction" } },
  { "term": { "genre": "tech" } }
] } } }

This returns fiction books and tech books, and nothing else. You can demand more with "minimum_should_match": 2 (at least two should clauses must match).

The shortcut

Sort each condition in the request into "deal-breaker" or "bonus". Deal-breakers go in must; bonuses go in should. If there are no deal-breakers at all, should becomes "match at least one".

"Books about code, preferably in stock and recent"Step 1 of 4
  1. Deal-breaker

    "about code" → must: match title with code.

Drills

Answer fast — trust the rule.

quick drill

A bool has one must and two should clauses. A document matches the must but neither should. Is it returned?

quick drill

A bool contains only two should clauses. What must a document do to be returned?

quick drill

Which clause both requires a match and adds to the score?

quick drill

You want recent books to rank higher without hiding older ones. Where does the date range go?

Remember

  • must is required and scored; should is optional and adds score.
  • Deal-breakers → must; bonuses → should.
  • With no must or filter, at least one should must match.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainDeal-breakers and bonuses

A shopper wants "books about code, preferably in stock and published after 2020". Explain which conditions go in must and which in should, what happens to a matching book that is out of stock, and how the query changes if the word "preferably" is removed.