A range query helps you find items where a value falls between two limits, like searching for prices between $10 and $50.
Range query in Elasticsearch
Introduction
Syntax
Elasticsearch
{
"range": {
"field_name": {
"gte": value1,
"lte": value2
}
}
}gte means 'greater than or equal to'.
lte means 'less than or equal to'. You can also use gt or lt for strict comparisons.
Examples
Elasticsearch
{
"range": {
"price": {
"gte": 10,
"lte": 50
}
}
}Elasticsearch
{
"range": {
"date": {
"gt": "2023-01-01",
"lt": "2023-12-31"
}
}
}Elasticsearch
{
"range": {
"age": {
"gte": 18
}
}
}Sample Program
This query searches for documents where the price field is between 20 and 100, including both 20 and 100.
Elasticsearch
{
"query": {
"range": {
"price": {
"gte": 20,
"lte": 100
}
}
}
}Important Notes
You can use gt and lt for strict greater than or less than.
Range queries work well with numbers, dates, and even strings that can be ordered.
Make sure the field you query supports range operations (like numbers or dates).
Summary
Range query finds values between limits.
Use gte, lte, gt, and lt to set boundaries.
Great for filtering by price, date, age, and more.
Practice
1. What does a
range query in Elasticsearch do?easy
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]
Hint: Range query filters values between limits using gte, lte, gt, lt [OK]
Common Mistakes:
- Confusing range query with count or update operations
- Thinking range query modifies data
- Assuming range query deletes documents
2. Which of the following is the correct syntax for a range query filtering documents where
price is greater than or equal to 10 and less than 20?easy
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]
Hint: Match gte/gt and lte/lt exactly to the question limits [OK]
Common Mistakes:
- Mixing up gte with gt or lte with lt
- Using wrong operators for boundaries
- Syntax errors like missing braces
3. Given this Elasticsearch query, what documents will it match?
{ "query": { "range": { "age": { "gt": 30, "lte": 40 } } } }medium
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]
Hint: Remember gt excludes boundary, lte includes boundary [OK]
Common Mistakes:
- Confusing gt with gte or lte with lt
- Assuming boundaries are inclusive when they are not
- Misreading the query operators
4. Identify the error in this range query that tries to find documents with
date between '2023-01-01' and '2023-12-31':{ "range": { "date": { "gte": "2023-01-01", "lt": "2023-12-31" } }medium
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]
Hint: Always check JSON braces and commas carefully [OK]
Common Mistakes:
- Forgetting to close JSON objects
- Assuming date strings must be timestamps
- Thinking gte and lt cannot be combined
5. You want to filter products priced between 50 and 100 inclusive, but exclude those exactly priced at 75. Which Elasticsearch query correctly applies this logic?
hard
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]
Hint: Use bool must for range and must_not to exclude values [OK]
Common Mistakes:
- Using invalid operators like 'neq' in range
- Not wrapping queries in bool for combining conditions
- Using should instead of must for filtering
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
