AI Hints
Home›Elasticsearch Tutorial›Ranges and Histograms
Elasticsearch · Lesson 73 of 120

range Aggregation

Price bands.

⏱ 2 min read▶ Video at 1:09:09🎓 Free
Watch this lesson in the Elasticsearch courseWatch on YouTube ↗Full Elasticsearch course

Shops show price bands, like under fifty, fifty to one fifty, and over one fifty, each with a count. The range aggregation makes exactly those buckets.

  • Under $50
  • $50 to $150
  • Over $150

Price bands

Each range has a from, a to, or both. From is included, and to is not. A key gives each bucket a friendly name. Run it. Three buckets, with a count of products in each.

Dev Tools Consoletext
GET products/_search?filter_path=aggregations.bands.buckets.key,aggregations.bands.buckets.doc_count
{
  "size": 0,
  "aggs": { "bands": { "range": { "field": "price", "ranges": [
    { "key": "under 50",  "to": 50 },
    { "key": "50 to 150", "from": 50, "to": 150 },
    { "key": "over 150",  "from": 150 } ] } } }
}
Output
{
  "aggregations": {
    "bands": {
      "buckets": [
        {"key": "under 50", "doc_count": 7},
        {"key": "50 to 150", "doc_count": 22},
        {"key": "over 150", "doc_count": 20}
      ]
    }
  }
}
Also uses _load
catalog

range notes

Ranges can overlap, and a document can land in more than one. From is inclusive, and to is exclusive. Date range works the same way for dates, with date maths like now minus thirty days.

keyvalue
frominclusive
toexclusive
Overlapallowed
date_rangenow-30d/d

Summary

The range aggregation buckets numbers into the bands you choose. Next, evenly sized buckets with histogram.

Key takeaways
  • range
  • from / to
  • key
Next up: histogram — equal-width buckets