📖 Scenario: You have a collection of products in an online store. Each product has a price. You want to find products within a certain price range.
🎯 Goal: Build an Elasticsearch range query to find products priced between 50 and 150.
📋 What You'll Learn
Create an Elasticsearch index mapping with a price field of type float.
Add a configuration variable for the price range minimum and maximum.
Write a range query using the range keyword with gte and lte for the price field.
Print the final query JSON.
💡 Why This Matters
🌍 Real World
Range queries are used in search engines to filter results by numbers like price, date, or ratings.
💼 Career
Knowing how to write range queries is important for roles involving search, data filtering, and Elasticsearch administration.
Progress0 / 4 steps
1
Create index mapping with price field
Create a variable called index_mapping and assign it a JSON object with a mappings key. Inside mappings, add properties with a price field of type float.
Elasticsearch
Hint
Use a dictionary with keys mappings and properties. Set price type to float.
2
Set price range variables
Create two variables called min_price and max_price and set them to 50 and 150 respectively.
Elasticsearch
Hint
Assign 50 to min_price and 150 to max_price.
3
Create the range query
Create a variable called range_query and assign it a JSON object with a query key. Inside query, use range on the price field with gte set to min_price and lte set to max_price.
Elasticsearch
Hint
Use the range keyword inside query with gte and lte set to the variables.
4
Print the range query
Write a print statement to display the range_query variable.
Elasticsearch
Hint
Use print(range_query) to show the query.
Practice
(1/5)
1. What does a range query in Elasticsearch do?
easy
A. It updates documents with new values.
B. It counts the total number of documents in the index.
C. It finds documents with field values between specified limits.
D. It deletes documents matching a condition.
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 C
Quick 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?
A. The field name 'date' must be inside a 'query' object.
B. The query is missing a closing brace '}' at the end.
C. The range query cannot use 'gte' and 'lt' together.
D. The date format is incorrect; it should be timestamps.
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 B
Quick 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?