must and should are often described as Elasticsearch’s versions of AND and OR. That shorthand is useful until both clauses appear in the same bool query.
A must clause is always required. A should clause may be required, or it may only improve the score of documents that already match. The difference depends on the other clauses in that particular bool query.
The examples below use five products and were checked against Elasticsearch 8.19.0:
| ID | Title | Description contains | Category | In stock |
|---|---|---|---|---|
| 1 | Trail running shoes | lightweight, waterproof | footwear | true |
| 2 | Road running shoes | lightweight | footwear | true |
| 3 | Trail hiking boots | waterproof | footwear | false |
| 4 | Running socks | lightweight | accessories | true |
| 5 | City backpack | waterproof | bags | true |
The short version
| Clause | Must it match? | Affects _score? | Typical use |
|---|---|---|---|
must | Yes | Yes | Required full-text conditions |
should | It depends | Yes | OR choices or relevance preferences |
filter | Yes | No | Required exact values, ranges, dates, and other structured conditions |
must_not | A match excludes the document | No | Prohibited values and other exclusions |
The Elasticsearch bool query reference describes must and should as scoring clauses. A document must satisfy every must clause. Elasticsearch sums the scores from matching must and should clauses to produce the final _score, so matching more clauses can move a document higher in the results.
How must works
Place multiple queries in the must array when every query must match:
GET /products/_search
{
"_source": ["title"],
"query": {
"bool": {
"must": [
{ "match": { "title": "running" } },
{ "term": { "category": "footwear" } }
]
}
}
}
This query returns IDs 1 and 2. Both contain running in the title and have footwear as the category. The running socks match the first clause but fail the second, so they are excluded.
Both clauses also contribute to _score. That matters for full-text relevance, but it is often unnecessary for a category, status, date, or other hard condition. We will handle that case with filter below.
How should works by itself
When a bool query contains should clauses and has no must or filter, Elasticsearch requires at least one should clause to match by default:
GET /products/_search
{
"_source": ["title"],
"query": {
"bool": {
"should": [
{ "match": { "title": "trail" } },
{ "match": { "title": "running" } }
]
}
}
}
This returns IDs 1, 2, 3, and 4. Each title contains trail, running, or both. ID 1 matches both clauses and received the highest score in the test index.
In this form, should behaves like OR for selecting documents. The default minimum_should_match value is 1 because this bool has no required clause.
Why should becomes optional next to must
Now require waterproof in the description and prefer products that are in stock:
GET /products/_search
{
"_source": ["title", "in_stock"],
"query": {
"bool": {
"must": [
{ "match": { "description": "waterproof" } }
],
"should": [
{ "term": { "in_stock": true } }
]
}
}
}
The query returns IDs 1, 3, and 5. ID 3 is out of stock, but it is still included because it satisfies the must clause. The in-stock products receive an extra scoring contribution from should and rank ahead of it.
This is the default that causes most must versus should confusion:
Clauses in the same bool | Default minimum_should_match |
|---|---|
One or more should, with no must or filter | 1 |
One or more should, plus must or filter | 0 |
The rule applies separately to each bool, including a nested one. A must in the parent query does not change the default inside a child bool.
Making should mandatory with minimum_should_match
Set minimum_should_match when a document must satisfy some of the should clauses:
GET /products/_search
{
"_source": ["title", "in_stock"],
"query": {
"bool": {
"must": [
{ "match": { "description": "waterproof" } }
],
"should": [
{ "term": { "in_stock": true } }
],
"minimum_should_match": 1
}
}
}
This version returns IDs 1 and 5. ID 3 still satisfies must, but it is removed because it does not satisfy the one required should clause.
However, the parameter proves to be more useful with larger OR groups. For example, this query requires any two of its three clauses:
GET /products/_search
{
"query": {
"bool": {
"should": [
{ "match": { "title": "running" } },
{ "match": { "description": "lightweight" } },
{ "match": { "description": "waterproof" } }
],
"minimum_should_match": 2
}
}
}
IDs 1, 2, and 4 each match at least two clauses. Elasticsearch also accepts percentages, negative values, and conditional expressions. The minimum_should_match reference documents those forms.
You can increase the influence of an individual clause with the boost parameter. Consider this example, where matching “waterproof” receives twice its default boost when Elasticsearch calculates _score:
GET /products/_search
{
"query": {
"bool": {
"should": [
{ "match": {"title": { "query" : "running"} }},
{ "match": {"description": "lightweight"} },
{ "match": {"description": {"query":"waterproof","boost": 2.0}} }
],
"minimum_should_match": 2
}
}
}
must does not mean every search word must appear
must controls whether its child query has to match. The child query still follows its own rules.
For example, a match query analyzes its input and uses OR between the resulting terms by default:
GET /products/_search
{
"query": {
"bool": {
"must": [
{ "match": { "title": "trail socks" } }
]
}
}
}
In the test index, this returns the two titles containing trail and the title containing socks. The match query itself matched because either analyzed term was present.
Set the inner query’s operator when both terms must appear:
{
"match": {
"title": {
"query": "trail socks",
"operator": "and"
}
}
}
No test document contains both words, so this version returns no results. must requires the whole match query. operator controls the terms produced inside that query.
Grouping AND and OR with a nested bool
Nested bool queries make the grouping explicit. The following query means (trail OR road) AND shoes:
GET /products/_search
{
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{ "match": { "title": "trail" } },
{ "match": { "title": "road" } }
],
"minimum_should_match": 1
}
},
{ "match": { "title": "shoes" } }
]
}
}
}
The inner bool accepts either trail or road. The outer must then requires that group and shoes, returning IDs 1 and 2. The explicit minimum_should_match: 1 is redundant here because the inner bool has no must or filter, so its default is already 1. Keeping it in the query makes the OR requirement visible. Elasticsearch supports deeper nesting, though its documentation recommends keeping bool queries as flat as practical.
Wide should arrays can add work. When several alternatives are exact values on the same field, a single terms query can express that OR in one clause. This substitution changes clause counting and does not support separate boosts for each value, so it is not a general replacement for the scored match clauses above.
Use filter for required conditions that should not score
A product category usually decides whether a document is allowed into the result set. It rarely says how relevant the document is. Put that condition in filter:
GET /products/_search
{
"query": {
"bool": {
"must": [
{ "match": { "title": "running" } }
],
"filter": [
{ "term": { "category": "footwear" } }
]
}
}
}
This returns the same two products as the earlier two-must query, but the category condition does not change _score. Filters run in filter context and may be cached. Elasticsearch’s query and filter context guide recommends query context for conditions that affect relevance and filter context for the rest.
must_not handles the other side of filter context. A document is excluded if it matches any must_not clause, and the clause does not contribute to _score. For example, a must_not clause containing { "term": { "in_stock": false } } removes out-of-stock products without changing the scores of the remaining results.
Use the clauses this way:
- Use
mustfor a required condition that should affect relevance. - Use
filterfor a required condition that should only include or exclude documents. - Use
must_notfor a required exclusion that should not affect relevance. - Use
shouldby itself for OR choices. - Use
shouldbesidemustorfilterfor optional relevance signals. - Set
minimum_should_matchwhenever thoseshouldclauses are requirements rather than preferences.
How purpose-built search engines achieve the same goals
Typesense separates these concerns into flatter search parameters. q and query_by define the full-text search, filter_by holds required structured conditions, and a condition inside _eval(...) can rank preferred records first without removing the others. The Typesense Search API calls the last pattern optional filtering.
For the earlier requirement, “description must match waterproof, but prefer in-stock products,” the Typesense search parameters are:
{
"q": "waterproof",
"query_by": "description",
"sort_by": "_eval(in_stock:true):desc,_text_match:desc"
}
All three waterproof products remain in the results, with the in-stock products first. This behavior was checked against Typesense 30.2 using the same five records.
If being in stock is mandatory, move the condition to filter_by:
{
"q": "waterproof",
"query_by": "description",
"filter_by": "in_stock:true"
}
You can combine hard conditions with && and ||, while field weights and _text_match control text relevance. The Typesense ranking guide describes those controls. The parameters cover the common application-search split between text that must match, filters that must pass, and conditions that should improve rank.
Conclusion
Elasticsearch’s bool query gives you precise control over required matches, optional preferences, and scoring. That flexibility also makes should easy to misread when must or filter is added, so set minimum_should_match explicitly when the distinction matters. For common application-search flows, Typesense puts text relevance in query_by, hard constraints in filter_by, and ranking preferences in _eval(...). The flatter request can be easier to read and maintain when the search does not require deeply nested boolean logic.