The Elasticsearch bool query combines several queries into one by sorting them into four clauses: must, filter, should and must_not. Use must and filter for conditions a document has to meet, must_not to exclude documents, and should for optional conditions that raise the score. Only must and should affect relevance scoring; filter and must_not just include or exclude.
Almost every real search combines a text match with exact conditions such as category, price or status. The bool query is how you express that in a single request.
A bool query is an object with up to four arrays, each holding other queries:
| Clause | Document must match? | Affects score? | Typical use |
|---|---|---|---|
must | Yes | Yes | Full-text search the user typed |
filter | Yes | No | Exact conditions: status, range, category |
should | Optional (see below) | Yes | Boosting preferred results |
must_not | Must not match | No | Excluding documents |
Think of must as AND with scoring, filter as AND without scoring, must_not as NOT, and should as OR. The clauses are themselves queries, so you can put a match, term, range or even another bool inside any of them.
Assume an index called products with a text field name, a keyword field category, a numeric price and a boolean in_stock. Create a few documents first:
Now search for wireless headphones that are in stock, in the audio category and cost less than 250, while excluding refurbished items:
Only document 1 is returned. Document 3 is out because it is out of stock and matches refurbished, and document 4 is out because its category is computers. The text match in must produces the relevance score; the three conditions in filter do not change it.
You can send each clause as a single object instead of an array when it has only one query, but arrays are the safer habit because you can add conditions without restructuring the request.
Both require a match. The difference is scoring: must contributes to _score, while filter runs in filter context, ignores scoring and can be cached by Elasticsearch. Put anything that is a yes or no question in filter, and keep must for text that should be ranked.
Documents matching any query in must_not are removed. Because it runs in filter context, it never changes scores and it is cheap to combine with other clauses.
The behaviour of should depends on what else is in the bool query:
must or filter clause, should is optional by default. Documents that match it get a higher score, but documents that do not are still returned.should clauses, at least one of them must match by default.You can control this with minimum_should_match, which accepts a number or a percentage:
Here a document must be in stock and match at least two of the three should clauses. Setting "minimum_should_match": 1 together with a filter turns should into a real OR.
Because every clause accepts any query, you can nest bool queries to express grouped logic such as A AND (B OR C):
The inner bool is an OR between an audio category and a low price; the outer query then requires that result together with the text match. Keep nesting shallow where you can. Deeply nested bool queries are hard to read and hard to debug.
A common pattern is to find all matching documents but rank some higher. Use should with a boost:
Every headphone product is returned, but in-stock and noise-cancelling products rise to the top. If you want to see why a document scored the way it did, add "explain": true to the request.
must. They still work, but they waste scoring work and skip filter caching. Move them to filter.term on an analysed text field. A term query is not analysed, so {"term": {"name": "Wireless"}} will not match the lowercase token stored in the index. Use match for text fields and term for keyword, numeric, date and boolean fields.should to be required. Next to a must or filter it only boosts, unless you set minimum_should_match.Both require a document to match. must also contributes to the relevance score, while filter does not and can be cached, so it is usually faster for exact conditions.
Yes. Any clause can contain another bool query, which is how you build grouped AND and OR logic.
Only when there is no must or filter in the same bool query, or when you set minimum_should_match to 1 or more. Otherwise it only increases the score of matching documents.
must and should add to _score. filter and must_not run in filter context and do not.
The bool query is the standard way to combine conditions in the Elasticsearch Query DSL. Use must for ranked text queries, filter for exact conditions, must_not to exclude, and should for optional boosts or OR logic controlled by minimum_should_match. Nest bool queries for grouped logic, and keep exact-value checks in filter for speed and caching.