Auto-interval Date Histogram Aggregation

A multi-bucket aggregation similar to the Date histogram aggregation except instead of providing an interval to use as the width of each bucket, a target number of buckets is provided indicating the number of buckets needed and the interval of the buckets is automatically chosen to best achieve that target. The number of buckets returned will always be less than or equal to this target number.

The buckets field is optional, and will default to 10 buckets if not specified.

Requesting a target of 10 buckets.

  1. POST /sales/_search?size=0
  2. {
  3. "aggs": {
  4. "sales_over_time": {
  5. "auto_date_histogram": {
  6. "field": "date",
  7. "buckets": 10
  8. }
  9. }
  10. }
  11. }

Keys

Internally, a date is represented as a 64 bit number representing a timestamp in milliseconds-since-the-epoch. These timestamps are returned as the bucket keys. The key_as_string is the same timestamp converted to a formatted date string using the format specified with the format parameter:

If no format is specified, then it will use the first date format specified in the field mapping.

  1. POST /sales/_search?size=0
  2. {
  3. "aggs": {
  4. "sales_over_time": {
  5. "auto_date_histogram": {
  6. "field": "date",
  7. "buckets": 5,
  8. "format": "yyyy-MM-dd"
  9. }
  10. }
  11. }
  12. }

Supports expressive date format pattern

Response:

  1. {
  2. ...
  3. "aggregations": {
  4. "sales_over_time": {
  5. "buckets": [
  6. {
  7. "key_as_string": "2015-01-01",
  8. "key": 1420070400000,
  9. "doc_count": 3
  10. },
  11. {
  12. "key_as_string": "2015-02-01",
  13. "key": 1422748800000,
  14. "doc_count": 2
  15. },
  16. {
  17. "key_as_string": "2015-03-01",
  18. "key": 1425168000000,
  19. "doc_count": 2
  20. }
  21. ],
  22. "interval": "1M"
  23. }
  24. }
  25. }

Intervals

The interval of the returned buckets is selected based on the data collected by the aggregation so that the number of buckets returned is less than or equal to the number requested. The possible intervals returned are:

seconds

In multiples of 1, 5, 10 and 30

minutes

In multiples of 1, 5, 10 and 30

hours

In multiples of 1, 3 and 12

days

In multiples of 1, and 7

months

In multiples of 1, and 3

years

In multiples of 1, 5, 10, 20, 50 and 100

In the worst case, where the number of daily buckets are too many for the requested number of buckets, the number of buckets returned will be 1/7th of the number of buckets requested.

Time Zone

Date-times are stored in Elasticsearch in UTC. By default, all bucketing and rounding is also done in UTC. The time_zone parameter can be used to indicate that bucketing should use a different time zone.

Time zones may either be specified as an ISO 8601 UTC offset (e.g. +01:00 or -08:00) or as a timezone id, an identifier used in the TZ database like America/Los_Angeles.

Consider the following example:

  1. PUT my-index-00001/log/1?refresh
  2. {
  3. "date": "2015-10-01T00:30:00Z"
  4. }
  5. PUT my-index-00001/log/2?refresh
  6. {
  7. "date": "2015-10-01T01:30:00Z"
  8. }
  9. PUT my-index-00001/log/3?refresh
  10. {
  11. "date": "2015-10-01T02:30:00Z"
  12. }
  13. GET my-index-00001/_search?size=0
  14. {
  15. "aggs": {
  16. "by_day": {
  17. "auto_date_histogram": {
  18. "field": "date",
  19. "buckets" : 3
  20. }
  21. }
  22. }
  23. }

UTC is used if no time zone is specified, three 1-hour buckets are returned starting at midnight UTC on 1 October 2015:

  1. {
  2. ...
  3. "aggregations": {
  4. "by_day": {
  5. "buckets": [
  6. {
  7. "key_as_string": "2015-10-01T00:00:00.000Z",
  8. "key": 1443657600000,
  9. "doc_count": 1
  10. },
  11. {
  12. "key_as_string": "2015-10-01T01:00:00.000Z",
  13. "key": 1443661200000,
  14. "doc_count": 1
  15. },
  16. {
  17. "key_as_string": "2015-10-01T02:00:00.000Z",
  18. "key": 1443664800000,
  19. "doc_count": 1
  20. }
  21. ],
  22. "interval": "1h"
  23. }
  24. }
  25. }

If a time_zone of -01:00 is specified, then midnight starts at one hour before midnight UTC:

  1. GET my-index-00001/_search?size=0
  2. {
  3. "aggs": {
  4. "by_day": {
  5. "auto_date_histogram": {
  6. "field": "date",
  7. "buckets" : 3,
  8. "time_zone": "-01:00"
  9. }
  10. }
  11. }
  12. }

Now three 1-hour buckets are still returned but the first bucket starts at 11:00pm on 30 September 2015 since that is the local time for the bucket in the specified time zone.

  1. {
  2. ...
  3. "aggregations": {
  4. "by_day": {
  5. "buckets": [
  6. {
  7. "key_as_string": "2015-09-30T23:00:00.000-01:00",
  8. "key": 1443657600000,
  9. "doc_count": 1
  10. },
  11. {
  12. "key_as_string": "2015-10-01T00:00:00.000-01:00",
  13. "key": 1443661200000,
  14. "doc_count": 1
  15. },
  16. {
  17. "key_as_string": "2015-10-01T01:00:00.000-01:00",
  18. "key": 1443664800000,
  19. "doc_count": 1
  20. }
  21. ],
  22. "interval": "1h"
  23. }
  24. }
  25. }

The key_as_string value represents midnight on each day in the specified time zone.

When using time zones that follow DST (daylight savings time) changes, buckets close to the moment when those changes happen can have slightly different sizes than neighbouring buckets. For example, consider a DST start in the CET time zone: on 27 March 2016 at 2am, clocks were turned forward 1 hour to 3am local time. If the result of the aggregation was daily buckets, the bucket covering that day will only hold data for 23 hours instead of the usual 24 hours for other buckets. The same is true for shorter intervals like e.g. 12h. Here, we will have only a 11h bucket on the morning of 27 March when the DST shift happens.

Scripts

Like with the normal date_histogram, both document level scripts and value level scripts are supported. This aggregation does not however, support the min_doc_count, extended_bounds and order parameters.

Minimum Interval parameter

The minimum_interval allows the caller to specify the minimum rounding interval that should be used. This can make the collection process more efficient, as the aggregation will not attempt to round at any interval lower than minimum_interval.

The accepted units for minimum_interval are:

  • year
  • month
  • day
  • hour
  • minute
  • second
  1. POST /sales/_search?size=0
  2. {
  3. "aggs": {
  4. "sale_date": {
  5. "auto_date_histogram": {
  6. "field": "date",
  7. "buckets": 10,
  8. "minimum_interval": "minute"
  9. }
  10. }
  11. }
  12. }

Missing value

The missing parameter defines how documents that are missing a value should be treated. By default they will be ignored but it is also possible to treat them as if they had a value.

  1. POST /sales/_search?size=0
  2. {
  3. "aggs": {
  4. "sale_date": {
  5. "auto_date_histogram": {
  6. "field": "date",
  7. "buckets": 10,
  8. "missing": "2000/01/01"
  9. }
  10. }
  11. }
  12. }

Documents without a value in the publish_date field will fall into the same bucket as documents that have the value 2000-01-01.