The date_histogram aggregation groups documents into time buckets, such as one bucket per hour, day or month, and gives you the count (or any metric) for each. It is the standard way to build a time series in Elasticsearch for charts of orders per day, errors per minute or sign-ups per month. You choose the bucket width with calendar_interval or fixed_interval, and Elasticsearch fills each bucket by the document's date value.
Getting a time series right depends on four details: the interval type, the time zone, empty buckets and the extended bounds. This guide covers each on Elasticsearch 8.x and 9.x.
The examples use an orders index with created_at (date) and total (float).
The key is the start of the bucket in epoch milliseconds, and key_as_string is the same moment formatted using the field's date format.
Elasticsearch has two interval types, and they cannot be mixed in one request.
| Type | Values | Behaviour |
|---|---|---|
calendar_interval | minute, hour, day, week, month, quarter, year | Follows the calendar, so months have 28 to 31 days and days can have 23 or 25 hours across daylight saving changes |
fixed_interval | multiples of ms, s, m, h, d, for example 30m, 6h, 7d | Always the same number of milliseconds |
Use calendar_interval when the label matters to a human ("March"), and fixed_interval when you need equal-width buckets, for example every 15 minutes. A calendar_interval accepts only a single unit, so 2d is invalid there; use fixed_interval: "2d". The old single interval parameter is deprecated, so use one of the two explicit parameters.
Dates are stored in UTC. Without a time zone, a "day" bucket starts at midnight UTC, which is the wrong midnight for most users.
Use IANA names such as Europe/Berlin rather than fixed offsets, so daylight saving is handled correctly. The bucket keys are still reported as UTC instants, but each bucket now spans the local day.
By default, min_doc_count is 0, so buckets between the first and last document are returned with a count of 0. That is what a chart needs. Buckets before the first and after the last document are not created, so use extended_bounds to force a range:
Now the response has 31 buckets, even if nothing happened on the first or last days. If you prefer to hide empty buckets, set "min_doc_count": 1.
Put a sub-aggregation inside to get revenue per day instead of only counts:
Each bucket now has doc_count and a revenue value, which is what a line chart needs.
A cumulative_sum pipeline aggregation turns daily revenue into a running total:
Other useful pipelines for time series are derivative (change per bucket) and moving_fn (moving average).
A date range with a very small interval creates many buckets. A year of data at one-minute intervals makes more than half a million buckets, which exceeds the search.max_buckets limit and fails. Pick an interval that matches the chart width: for example, hour for one day, day for one month, week or month for a year. Kibana uses the auto_date_histogram aggregation for this, which takes a target bucket count and picks the interval for you:
The default time zone is UTC. Set time_zone to the zone your users expect.
No, calendar_interval only accepts one unit. Use fixed_interval for equal widths such as 14d, or group the months yourself in the application.
Empty buckets between documents are returned by default. If you see gaps at the edges, add extended_bounds. If you see no empty buckets inside, check that you did not set min_doc_count to 1.
Only for key_as_string output. Bucketing uses the stored instant, so you can change format for display without changing the groups.
date_histogram groups documents into time buckets using a calendar_interval or a fixed_interval. Always set time_zone for user-facing charts, use extended_bounds to fill the edges, and nest metric or pipeline aggregations to build revenue lines and running totals. Keep the bucket count within search.max_buckets by choosing an interval that fits the time range.