A filter context answers only whether a document matches. Elasticsearch returns matching documents without calculating relevance scores, so their _score is 0.0. A query determines the execution context: match queries generally use query context, while filter clauses use filter context. section 8.4.2
Filtering movie matches
To find movies with “Godfather” in the title without ranking them, place the match query inside a bool filter clause: GET movies/_search { "query": { "bool": { "filter": [ { "match": { "title": "Godfather" } } ] } } } The two matching movies remain included, but both receive a score of 0.0. Use this form when matching is sufficient and relevance ordering is unnecessary. section 8.4.2
Filter context also appears in compound queries:
- The must_not clause excludes matching documents and does not alter scores produced by other clauses. section 11.3.5
- Filter execution avoids the relevance-scoring algorithm and is therefore performant for yes-or-no criteria. section 11.3.6
- Wrap a filter in constant_score when every matching document needs a chosen nonzero score, such as a boost for black TVs. section 11.4
Inspect the runnable bool query that places a movie match in filter context and demonstrates zero scores. section 8.4.2
Listing 11.17 A constant_score query that produces a static score
GET products/_search
{
"query": {
"constant_score": { #1
"filter": { #2
"range": {
"user_ratings": {
"gte": 4,
"lte": 5
}
}
},
"boost": 5.0 #3
}
}
}
Inspect the constant_score form, which preserves filter-style matching while assigning a static boosted score. section 11.4
