Range Query
What it is
A range query selects documents where a field value falls within specified inclusive or exclusive boundaries. Ranges apply to numeric fields (integers, floats, dates), lexicographic order (strings), or IP addresses.
How it works
Range queries use field value indexing structures:
- Inverted index enumeration: For string ranges, enumerate sorted terms within bounds and union postings (slow for large ranges).
- DocValues/BKD trees: Modern systems store values separately (docvalues), enabling efficient range filtering via tree-based structures. BKD (Bkd-tree) for numerics enables O(log N) range lookups.
- Segment-level filtering: Skip segments where min/max values fall outside range.
[illustrate: BKD tree node pruning for range query [1000, 5000] on numeric field]
Example
Query: date:[2020-01-01 TO 2025-12-31] (inclusive on both bounds)
Query: price:{100.0 TO 500.0} (exclusive on both bounds)
Query: name:[a TO m] (lexicographic range)
Variants and history
Inclusive range: [low TO high]. Exclusive range: {low TO high}. Open-ended: [100 TO *]. SQL uses BETWEEN and comparison operators. Lucene uses bracket notation. Elasticsearch query DSL offers range queries with gte, lte, gt, lt. BKD-trees (introduced in Lucene 6) dramatically improved numeric range performance.
When to use it
Standard for numeric filtering (price, date, quantity). Essential for faceted search and parametric retrieval. Use with docvalues for performance on large indices. Lexicographic ranges less common but useful for string comparisons.