0
0
Elasticsearchquery~10 mins

Range query in Elasticsearch - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Range query
Start Range Query
Specify Field
Set Range Conditions
gte
Execute Query
Return Matching Documents
End
The range query starts by choosing a field, then sets conditions like greater than or less than, executes the query, and returns matching documents.
Execution Sample
Elasticsearch
{
  "query": {
    "range": {
      "age": { "gte": 30, "lt": 40 }
    }
  }
}
This query finds documents where the 'age' field is greater than or equal to 30 and less than 40.
Execution Table
StepActionFieldConditionEvaluationResult
1Start query---Ready to build range query
2Specify fieldage--Field set to 'age'
3Set condition gteagegte: 30Check if age >= 30Condition added
4Set condition ltagelt: 40Check if age < 40Condition added
5Execute queryagegte:30 and lt:40Filter documents by conditionsDocuments filtered
6Return results---Documents with 30 <= age < 40 returned
7End---Query complete
💡 All conditions applied and matching documents returned
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
fieldundefinedageageageage
conditionsemptyempty{"gte":30}{"gte":30, "lt":40}{"gte":30, "lt":40}
resultnonenonenonenonedocuments with age >=30 and <40
Key Moments - 2 Insights
Why do we specify both 'gte' and 'lt' instead of just one?
Using both 'gte' and 'lt' defines a range between two values. The execution_table rows 3 and 4 show adding each condition separately to narrow down the results.
What happens if we only use 'gte' without 'lt'?
The query will return all documents with the field value greater than or equal to the specified number, without an upper limit. This is shown by the condition in row 3 alone.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the field set to after step 2?
Agte
Bage
Cundefined
Dlt
💡 Hint
Check the 'Field' column in row with Step 2
At which step are both range conditions applied together?
AStep 5
BStep 3
CStep 4
DStep 6
💡 Hint
Look at the 'Condition' and 'Evaluation' columns in the execution_table rows
If we remove the 'lt' condition, how does the 'conditions' variable change after step 4?
A{}
B{"lt":40}
C{"gte":30}
D{"gte":30, "lt":40}
💡 Hint
Refer to variable_tracker 'conditions' row after Step 4
Concept Snapshot
Range query syntax:
{
  "query": {
    "range": {
      "field": { "gte": value1, "lt": value2 }
    }
  }
}
Use 'gte' for greater or equal, 'lt' for less than.
Combines conditions to filter documents within a range.
Full Transcript
The range query in Elasticsearch filters documents by checking if a field's value falls within specified limits. First, you choose the field to check. Then you add conditions like 'gte' (greater than or equal) and 'lt' (less than) to define the range. The query runs and returns documents matching these conditions. For example, setting 'age' with 'gte' 30 and 'lt' 40 returns documents where age is between 30 and 39. The execution table shows each step: starting the query, setting the field, adding conditions, executing, and returning results. Variables track the field name, conditions, and final results. Beginners often wonder why both 'gte' and 'lt' are needed; using both narrows the range precisely. If only 'gte' is used, results have no upper limit. The visual quiz checks understanding of these steps and variable changes.