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

date_histogram

Sales over time.

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

The most common chart in any dashboard is something over time. Orders per day, revenue per month. Date histogram buckets documents by calendar time.

  • Over time
  • Per day, per month
  • date_histogram

Orders per month

Date histogram on the order date, with a calendar interval of one month. The format makes the keys readable. Run it. Ten months of twenty twenty six, with the number of orders in each.

Dev Tools Consoletext
GET orders/_search?filter_path=aggregations.per_month.buckets.key_as_string,aggregations.per_month.buckets.doc_count
{
  "size": 0,
  "aggs": { "per_month": { "date_histogram": {
    "field": "date", "calendar_interval": "month", "format": "yyyy-MM" } } }
}
Output
{
  "aggregations": {
    "per_month": {
      "buckets": [
        {"key_as_string": "2026-01", "doc_count": 202},
        {"key_as_string": "2026-02", "doc_count": 171},
        {"key_as_string": "2026-03", "doc_count": 205},
        {"key_as_string": "2026-04", "doc_count": 226},
        {"key_as_string": "2026-05", "doc_count": 223},
        {"key_as_string": "2026-06", "doc_count": 188},
        {"key_as_string": "2026-07", "doc_count": 206},
        {"key_as_string": "2026-08", "doc_count": 214},
        {"key_as_string": "2026-09", "doc_count": 202},
        {"key_as_string": "2026-10", "doc_count": 163}
      ]
    }
  }
}
Also uses _load
orders

Time buckets

Calendar interval follows the calendar, so months have different lengths. Fixed interval uses exact sizes, like twelve hours. Time zone makes days start at your local midnight. And min doc count of zero fills in quiet days.

keyvalue
calendar_intervalday, week, month
fixed_interval12h, 30m
time_zonelocal midnight
Gapsmin_doc_count: 0

Summary

Date histogram buckets documents by day, week or month, the backbone of every time chart. Next, putting aggregations inside other aggregations.

Key takeaways
  • date_histogram
  • calendar_interval
  • Time charts
Next up: Sub-aggregations — numbers inside buckets