0
0
Elasticsearchquery~3 mins

Why Range query in Elasticsearch? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

Discover how a simple range query can save you hours of tedious searching!

The Scenario

Imagine you have a huge list of products with prices and you want to find all products priced between $50 and $100.

Without a range query, you might try to check each product one by one manually or write many separate conditions for each price.

The Problem

Manually checking each item or writing many conditions is slow and tiring.

It's easy to make mistakes, miss some prices, or write very long and confusing code.

The Solution

The range query lets you ask Elasticsearch to find all items within a price range quickly and simply.

You just say the minimum and maximum values once, and Elasticsearch does the hard work for you.

Before vs After
Before
{ "query": { "bool": { "should": [ { "term": { "price": 50 } }, { "term": { "price": 51 } }, { "term": { "price": 52 } }, { "term": { "price": 53 } }, { "term": { "price": 54 } }, { "term": { "price": 55 } }, { "term": { "price": 56 } }, { "term": { "price": 57 } }, { "term": { "price": 58 } }, { "term": { "price": 59 } }, { "term": { "price": 60 } }, { "term": { "price": 61 } }, { "term": { "price": 62 } }, { "term": { "price": 63 } }, { "term": { "price": 64 } }, { "term": { "price": 65 } }, { "term": { "price": 66 } }, { "term": { "price": 67 } }, { "term": { "price": 68 } }, { "term": { "price": 69 } }, { "term": { "price": 70 } }, { "term": { "price": 71 } }, { "term": { "price": 72 } }, { "term": { "price": 73 } }, { "term": { "price": 74 } }, { "term": { "price": 75 } }, { "term": { "price": 76 } }, { "term": { "price": 77 } }, { "term": { "price": 78 } }, { "term": { "price": 79 } }, { "term": { "price": 80 } }, { "term": { "price": 81 } }, { "term": { "price": 82 } }, { "term": { "price": 83 } }, { "term": { "price": 84 } }, { "term": { "price": 85 } }, { "term": { "price": 86 } }, { "term": { "price": 87 } }, { "term": { "price": 88 } }, { "term": { "price": 89 } }, { "term": { "price": 90 } }, { "term": { "price": 91 } }, { "term": { "price": 92 } }, { "term": { "price": 93 } }, { "term": { "price": 94 } }, { "term": { "price": 95 } }, { "term": { "price": 96 } }, { "term": { "price": 97 } }, { "term": { "price": 98 } }, { "term": { "price": 99 } }, { "term": { "price": 100 } } ] } } }
After
{ "query": { "range": { "price": { "gte": 50, "lte": 100 } } } }
What It Enables

With range queries, you can easily filter data by numbers, dates, or any ordered values, making your searches fast and precise.

Real Life Example

Online stores use range queries to let customers find products within their budget or filter events happening between certain dates.

Key Takeaways

Manual filtering by many values is slow and error-prone.

Range queries let you specify limits simply and clearly.

This makes searching large data sets efficient and easy.