Elasticsearch's boolean queries support must, must_not, should, and filter operations. The filter clause executes in filter context, which exclueds scoring and enables query result caching. This approach offers performance benefits for simple filtering requirements.
Filter context applies to various query types beyond bool filters, including must_not clauses, constant_score filters, and aggregation filters. These queries skip relevance scoring, focusing solely on document matching.
Query caching operates through this process:
- Document Matching: The inverted index identifies documents matching the filter terms
- Bitset Generation: A RoaringBitmap bitset is created where 1 indicates matching documents
- Bitset Iteration: The system processes the sparsest bitset first to maximize efficiency
- Caching Decision: Frequently used queries (typically appearing in recent 256 queries) are cached
Lucene manages query caching with Elasticsearch providing policy controls. The UsageTrackingQueryCachingPolicy employs LRU eviction. Segments with fewer than 10,000 doucments or comprising less than 3% of total index size are excluded from caching.
Caching eligibility follows these rules:
- Query must be a filter type
- TermQuery, MatchAllDocsQuery, MatchNoDocsQuery, and empty BooleanQuery/DisjunctionMaxQuery are excluded
- Costly queries (MultiTermQuery, TermInSetQuery, PointQuery) require 2+ occurrences
- Other filter queries require 5+ occurrences
Default node configuration limits query cache to 10,000 entries or 10% heap memory:
indices.queries.cache.count: 10000
indices.queries.cache.size: 10%
Manual cache clearance: POST /<index>/_cache/clear?query=true
Monitoring through Nodes/Index stats API:
"query_cache": {
"memory_size_in_bytes": 1110305640,
"total_count": 45109997,
"hit_count": 1192144,
"miss_count": 43917853,
"cache_size": 1309,
"cache_count": 51509,
"evictions": 50200
}
Consider monitoring hit ratio (hit_count/total_count) and investigating frequent evictions which may cause query performance variability. Filter context with query caching provides optimal performance for pure filtering scenarios.