Query DSLmedium3-5 years

A product search puts `{ "term": { "status": "PUBLISHED" } }` inside a `bool` query's `must` clause alongside the user's search text. What's wasted by doing that, and what's the fix?

Every clause in Elasticsearch runs in either query context (contributes to the relevance score) or filter context (yes/no match, cached, no scoring). A status: PUBLISHED condition is the same for every matching document — it carries zero information about how well a document matches, so scoring it wastes CPU computing a BM25 contribution that's identical across every result and doesn't change the ranking at all. Filter context clauses are also cached as reusable bitsets across queries, which must never gets. The fix is to move it to bool's filter array: same yes/no matching behaviour, no wasted scoring, and the result is cacheable.

The lesson behind it →
More on Query DSL