Range query in Elasticsearch - Time & Space Complexity
When we use a range query in Elasticsearch, we want to find documents with values between certain limits.
We ask: how does the time to find these documents grow as the data size grows?
Analyze the time complexity of the following code snippet.
GET /products/_search
{
"query": {
"range": {
"price": {
"gte": 10,
"lte": 50
}
}
}
}
This query finds all products with a price between 10 and 50 inclusive.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Elasticsearch scans the index to find matching documents within the price range.
- How many times: It checks each document's price once, but uses an index structure to avoid scanning all documents.
As the number of documents grows, Elasticsearch uses its index to quickly find matches without checking every document.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 3-4 checks using the index |
| 100 | About 7-8 checks using the index |
| 1000 | About 10-12 checks using the index |
Pattern observation: The number of operations grows slowly, not directly with the number of documents.
Time Complexity: O(log n + k)
This means the time to find documents grows slowly as the data grows, thanks to the index, plus a factor proportional to the number of matching documents.
[X] Wrong: "Range queries check every document one by one, so time grows directly with data size."
[OK] Correct: Elasticsearch uses indexes that let it jump quickly to matching documents without scanning all data.
Understanding how range queries scale helps you explain how search engines stay fast even with lots of data.
"What if we removed the index on the price field? How would the time complexity change?"
Practice
range query in Elasticsearch do?Solution
Step 1: Understand the purpose of range query
A range query filters documents where a field's value falls within given boundaries like greater than or less than.Step 2: Compare with other query types
Counting, updating, or deleting documents are different operations, not done by range queries.Final Answer:
It finds documents with field values between specified limits. -> Option CQuick Check:
Range query = filter by value limits [OK]
- Confusing range query with count or update operations
- Thinking range query modifies data
- Assuming range query deletes documents
price is greater than or equal to 10 and less than 20?Solution
Step 1: Identify the boundary conditions
The question asks for price >= 10 and price < 20, which means gte:10 and lt:20.Step 2: Match syntax with conditions
{ "range": { "price": { "gte": 10, "lt": 20 } } } uses gte and lt correctly matching the condition.Final Answer:
{ "range": { "price": { "gte": 10, "lt": 20 } } } -> Option DQuick Check:
gte=10 and lt=20 syntax = { "range": { "price": { "gte": 10, "lt": 20 } } } [OK]
- Mixing up gte with gt or lte with lt
- Using wrong operators for boundaries
- Syntax errors like missing braces
{ "query": { "range": { "age": { "gt": 30, "lte": 40 } } } }Solution
Step 1: Interpret the range operators
"gt": 30 means age > 30 (not including 30), "lte": 40 means age ≤ 40 (including 40).Step 2: Understand the matching documents
Documents with age greater than 30 and less than or equal to 40 will match.Final Answer:
Documents where age is greater than 30 and less than or equal to 40. -> Option AQuick Check:
gt=30 and lte=40 means age >30 and ≤ 40 [OK]
- Confusing gt with gte or lte with lt
- Assuming boundaries are inclusive when they are not
- Misreading the query operators
date between '2023-01-01' and '2023-12-31':{ "range": { "date": { "gte": "2023-01-01", "lt": "2023-12-31" } }Solution
Step 1: Check JSON syntax
The query is missing the final closing brace '}' to close the outer object.Step 2: Validate other parts
Date format as string is valid in Elasticsearch; using gte and lt together is allowed; 'date' field can be directly inside range.Final Answer:
The query is missing a closing brace '}' at the end. -> Option BQuick Check:
Missing closing brace = syntax error [OK]
- Forgetting to close JSON objects
- Assuming date strings must be timestamps
- Thinking gte and lt cannot be combined
Solution
Step 1: Understand the filtering requirements
We want prices between 50 and 100 inclusive, so gte:50 and lte:100 in a range query.Step 2: Exclude price exactly 75
Use a bool query with must for range and must_not for term price=75 to exclude it.Step 3: Check options
{ "bool": { "must": { "range": { "price": { "gte": 50, "lte": 100 } } }, "must_not": { "term": { "price": 75 } } } } uses bool with must range and must_not term correctly. { "range": { "price": { "gte": 50, "lte": 100, "neq": 75 } } } uses invalid 'neq'. { "range": { "price": { "gt": 50, "lt": 100 } }, "must_not": { "term": { "price": 75 } } } misses bool wrapper. { "bool": { "should": { "range": { "price": { "gte": 50, "lte": 100 } } }, "must_not": { "term": { "price": 75 } } } } uses should which is incorrect here.Final Answer:
{ "bool": { "must": { "range": { "price": { "gte": 50, "lte": 100 } } }, "must_not": { "term": { "price": 75 } } } } -> Option AQuick Check:
Use bool must + must_not for inclusive range excluding a value [OK]
- Using invalid operators like 'neq' in range
- Not wrapping queries in bool for combining conditions
- Using should instead of must for filtering
Ready to prove it?
Pass the test and this pattern moves unknown
