Skip to the workspace
Pebellslearn by building
Controlling Search Results  ›  Lesson

bool: filter and must_not — Query vs Filter Context

filter 12 minClauses that ask how well a document matches run in scored query context, while yes-or-no clauses belong in unscored, cacheable filter and must_not.

elasticsearch / bool: filter and must_not — Query vs Filter Context

Explore each scene at your own pace.

01 · The rule

The rule

bool: filter and must_not — Query vs Filter Context

"Garden books under 20 that are in stock." Only one part of that sentence is about relevance — how well a title matches "garden". The price limit and stock flag are plain yes-or-no tests. Putting yes-or-no tests in the right place makes searches faster and rankings cleaner.

The rule

Ask "how well?" in query context; ask "yes or no?" in filter context.

Every clause runs in one of two contexts:

  • Query context — "how well does this document match?" It computes a relevance _score. must and should run here.
  • Filter context — "does it match, yes or no?" No score is computed, and Elasticsearch can cache the result, so repeated filters are cheap. filter and must_not run here.

The two filter-context clauses:

  • filter — every query listed must match, but adds nothing to the score.
  • must_not — any document matching a listed query is excluded.
GET /books/_search
{
  "query": {
    "bool": {
      "must":     [ { "match": { "title": "garden" } } ],
      "filter":   [ { "range": { "price": { "lt": 20 } } },
                    { "term":  { "in_stock": true } } ],
      "must_not": [ { "term":  { "genre": "hobby" } } ]
    }
  }
}

Only the match affects ranking. Price, stock and genre just decide who gets in. If you moved the price range into must, the results would be the same set, but every score would shift by a meaningless bonus for "matching the price" — and the check could not be cached.

A bool with only filter clauses returns every hit with a score of 0 — the right choice when you just want "all fiction books", with order coming from sorting instead.

must_not is also how you negate other leaves, such as books with no price:

{ "query": { "bool": { "must_not": [ { "exists": { "field": "price" } } ] } } }
Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.