When a product search must return only matching documents without ranking them by the restriction, place the condition in a bool query’s filter clause. It behaves like must for matching, but runs in filter context: results receive no scores, and Elasticsearch can cache them for better performance. section 11.3.7 section 11.3
Filter TVs by price
A filter can combine a term condition with a price range: match the exact product value TV, then require a price from 500 through 1,000. The returned documents have a score of 0.0 because no relevance scoring is required. section 11.3.7
Use filter clauses for fixed search constraints:
- Combine a must clause for relevance with a filter for exact constraints, such as LG products priced from 500 through 1,000. section 11.3.7
- Combine filter with must_not and should when a search has exclusions, optional preferences, and a non-scoring price boundary. section 11.3.8
- Keep relevance-bearing text queries in must or should, because filter conditions do not affect document ranking. section 11.3.6 section 11.3.7
Listing 11.12 The filter clause
GET products/_search
{
"query": {
"bool": {
"filter": [{ #1
"term": {
"product.keyword": "TV"
},
{
"range": {
"price": {
"gte": 500,
"lte": 1000
}
}
}
}
]
}
}
}
Inspect the focused REST example combining an exact product condition and a price range inside filter. section 11.3.7
Listing 11.14 All the clauses combined
GET products/_search
{
"query": {
"bool": {
"must": [{
"match": {
"brand": "LG"
}
}],
"must_not": [{
"term": {
"colour": "silver"
}
}],
"should": [{
"match": {
"energy_rating": "A++"
}
},
{
"term": {
"type": "Fridge Freezer"
}
}],
"filter": [{
"range": {
"price": {
"gte": 500,
"lte": 1000
}
}
}
]
}
}
}
Inspect a complete bool query that combines must, must_not, should, and filter clauses for a realistic product search. section 11.3.8
