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
