liveBook · Manning

Must_not clause

As explained in Elasticsearch in Action, Second Edition

A Boolean query clause in Elasticsearch that excludes documents matching its inner query.

A `must_not` clause excludes documents whose inner queries match. Place it inside a `bool` query when a result must satisfy positive criteria while avoiding unwanted values—for example, books by an author but not below a required rating. section 2.5.3 section 2.5.1

The clause accepts an array of leaf queries. Its logic is negation: a query that would normally find a document instead removes that document when wrapped by `must_not`. Used alone, it can return every document except those matching the excluded query, so pair it with `must` when the search needs a defined product type or other positive scope. section 11.3.4 section 11.3

Find 4K TVs in a price range, excluding brands and low ratings

```json GET products/_search { "query": { "bool": { "must": [ { "match": { "product": "TV" } }, { "term": { "resolution": { "value": "4k" } } }, { "range": { "price": { "gte": 500, "lte": 700 } } } ], "must_not": [ { "terms": { "brand.keyword": ["Philips", "Samsung"] } }, { "range": { "user_ratings": { "lte": 4.0 } } } ] } } } ``` `must` first establishes the TV, resolution, and price requirements. Each query in `must_not` then removes matching documents: excluded brands and ratings at or below 4.0. section 11.3.5

Use `must_not` for exclusion rules that should not affect relevance scoring:

  • Hide unavailable products from a shopping search. section 11.3.4
  • Remove books with ratings below a threshold while retaining an author match. section 2.5.3
  • Exclude a color while combining `must`, `should`, and `filter` requirements in one bool query. section 11.3.8

`must_not` runs in filter context and produces a binary include-or-exclude decision. It does not change the relevance score; scoring can instead come from `must` or `should` clauses. Add multiple leaf queries as requirements grow, but keep a positive `must` or other scope clause when the intended result set is narrower than “everything except these matches.” section 11.3.5 section 11.3.8

Listing 11.7 Enhancing the must_not query
GET products/_search
{
 "query": {
   "bool": {
     "must_not": [             #1
       {
         "terms": {            #2
           "brand.keyword": [
             "Philips",
             "Samsung"
           ]
         }
       },
       {
         "range": {            #3
           "user_ratings": {
             "lte": 4.0
           }
         }
       }
     ],
     "must": [                 #4
       {
         "match": {
           "product": "TV"
         }
       },
       {
         "term": {
           "resolution": {
             "value": "4k"
           }
         }
       },
       {
         "range": {
           "price": {
             "gte": 500,
             "lte": 700
           }
         }
       }
     ]
   }
 }
}

Inspect a full bool query where positive TV criteria are combined with two independent `must_not` exclusions. section 11.3.5

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Defines negation, demonstrates single and multiple leaf-query exclusions, and explains filter-context scoring behavior.
Examples
Demonstrates rating-filtered books, excluded TV brands, low-rating exclusions, hidden out-of-stock products, and omitted silver products.
What it assumes
Introduces must_not through bool-query clauses and leaf queries without requiring prior query construction in the examples.
What you can do after
Apply it via Elasticsearch bool queries to remove matching documents without changing relevance scores.
How it connects
Connects Boolean queries with must, should, filter, range, terms, term, and match queries.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elasticsearch in Action, Second Edition. The section links point to the parts of the book it draws on.