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

histogram

Equal-width buckets.

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

What does the price distribution look like? A histogram splits prices into equal width buckets, like every fifty dollars, and counts each one.

  • Distribution
  • Equal widths
  • Counts

Prices every 100 dollars

Histogram on price, with an interval of one hundred. Each bucket key is the start of its range. Run it. Most products are under two hundred dollars.

Dev Tools Consoletext
GET products/_search?filter_path=aggregations.prices.buckets
{
  "size": 0,
  "aggs": {
    "prices": { "histogram": { "field": "price", "interval": 100 } }
  }
}
Output
{
  "aggregations": {
    "prices": {
      "buckets": [
        {"key": 0.0, "doc_count": 18},
        {"key": 100.0, "doc_count": 17},
        {"key": 200.0, "doc_count": 10},
        {"key": 300.0, "doc_count": 2},
        {"key": 400.0, "doc_count": 0},
        {"key": 500.0, "doc_count": 2}
      ]
    }
  }
}
Also uses _load
catalog

histogram options

Interval sets the bucket width. Min doc count of zero keeps empty buckets, so charts have no gaps. Extended bounds forces the first and last bucket. And it's perfect for drawing bar charts.

keyvalue
intervalbucket width
min_doc_count: 0keep empty buckets
extended_boundsfix the range
Usebar charts

Summary

Histogram buckets numbers into equal widths, great for charts. Next, the same idea for dates.

Key takeaways
  • histogram
  • interval
  • Charts
Next up: date_histogram — sales over time