0
0
Elasticsearchquery~5 mins

Range query in Elasticsearch

Choose your learning style9 modes available
Introduction

A range query helps you find items where a value falls between two limits, like searching for prices between $10 and $50.

Finding products within a certain price range in an online store.
Searching for events happening between two dates.
Filtering documents created after a specific date.
Getting users with ages between 18 and 30.
Finding measurements that are above or below a threshold.
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
Finds items with price between 10 and 50, including 10 and 50.
Elasticsearch
{
  "range": {
    "price": {
      "gte": 10,
      "lte": 50
    }
  }
}
Finds documents with dates after Jan 1, 2023 and before Dec 31, 2023 (not including those dates).
Elasticsearch
{
  "range": {
    "date": {
      "gt": "2023-01-01",
      "lt": "2023-12-31"
    }
  }
}
Finds users aged 18 or older.
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
      }
    }
  }
}
OutputSuccess
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.