What is the difference between query context and filter context in Elasticsearch?

Answer

In Elasticsearch, query context asks "how well does this document match?" — it calculates a relevance score for each matching document. Use query context for full-text search where ranking by relevance matters (e.g., match query). Filter context asks "does this document match?" — it is a yes/no decision with no scoring, and results are automatically cached. Use filter context for structured data like status, date ranges, and boolean flags. In a bool query, clauses inside must and should contribute to the score (query context), while clauses inside filter and must_not do not (filter context). Always put non-scoring criteria in the filter clause for better performance.