2013-11-24 06:13:08 -05:00
[[search-aggregations-bucket-datehistogram-aggregation]]
2014-05-12 19:35:58 -04:00
=== Date Histogram Aggregation
2013-11-24 06:13:08 -05:00
2013-12-01 19:54:42 -05:00
A multi-bucket aggregation similar to the <<search-aggregations-bucket-histogram-aggregation,histogram>> except it can
only be applied on date values. Since dates are represented in elasticsearch internally as long values, it is possible
2014-01-17 11:20:05 -05:00
to use the normal `histogram` on dates as well, though accuracy will be compromised. The reason for this is in the fact
2013-12-01 19:54:42 -05:00
that time based intervals are not fixed (think of leap years and on the number of days in a month). For this reason,
2014-12-23 13:19:25 -05:00
we need special support for time based data. From a functionality perspective, this histogram supports the same features
2014-01-17 11:20:05 -05:00
as the normal <<search-aggregations-bucket-histogram-aggregation,histogram>>. The main difference is that the interval can be specified by date/time expressions.
2013-11-24 06:13:08 -05:00
2014-01-17 11:20:05 -05:00
Requesting bucket intervals of a month.
2013-11-24 06:13:08 -05:00
[source,js]
--------------------------------------------------
2017-01-20 16:23:28 -05:00
POST /sales/_search?size=0
2013-11-24 06:13:08 -05:00
{
"aggs" : {
2017-01-20 16:23:28 -05:00
"sales_over_time" : {
2013-11-24 06:13:08 -05:00
"date_histogram" : {
"field" : "date",
"interval" : "month"
}
}
}
}
--------------------------------------------------
2017-01-20 16:23:28 -05:00
// CONSOLE
// TEST[setup:sales]
2013-11-24 06:13:08 -05:00
2014-11-08 11:49:09 -05:00
Available expressions for interval: `year`, `quarter`, `month`, `week`, `day`, `hour`, `minute`, `second`
2016-06-29 17:02:15 -04:00
Time values can also be specified via abbreviations supported by <<time-units,time units>> parsing.
Note that fractional time values are not supported, but you can address this by shifting to another
time unit (e.g., `1.5h` could instead be specified as `90m`).
2013-11-24 06:13:08 -05:00
[source,js]
--------------------------------------------------
2017-01-20 16:23:28 -05:00
POST /sales/_search?size=0
2013-11-24 06:13:08 -05:00
{
"aggs" : {
2017-01-20 16:23:28 -05:00
"sales_over_time" : {
2013-11-24 06:13:08 -05:00
"date_histogram" : {
"field" : "date",
2016-06-29 17:02:15 -04:00
"interval" : "90m"
2013-11-24 06:13:08 -05:00
}
}
}
}
--------------------------------------------------
2017-01-20 16:23:28 -05:00
// CONSOLE
// TEST[setup:sales]
2013-11-24 06:13:08 -05:00
==== Keys
2015-09-07 13:53:50 -04:00
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
++key++s. The `key_as_string` is the same timestamp converted to a formatted
date string using the format specified with the `format` parameter:
TIP: If no `format` is specified, then it will use the first date
<<mapping-date-format,format>> specified in the field mapping.
2013-11-24 06:13:08 -05:00
[source,js]
--------------------------------------------------
2017-01-20 16:23:28 -05:00
POST /sales/_search?size=0
2013-11-24 06:13:08 -05:00
{
"aggs" : {
2017-01-20 16:23:28 -05:00
"sales_over_time" : {
2013-11-24 06:13:08 -05:00
"date_histogram" : {
"field" : "date",
"interval" : "1M",
"format" : "yyyy-MM-dd" <1>
}
}
}
}
--------------------------------------------------
2017-01-20 16:23:28 -05:00
// CONSOLE
// TEST[setup:sales]
2013-11-24 06:13:08 -05:00
<1> Supports expressive date <<date-format-pattern,format pattern>>
Response:
[source,js]
--------------------------------------------------
{
2017-01-20 16:23:28 -05:00
...
2013-11-24 06:13:08 -05:00
"aggregations": {
2017-01-20 16:23:28 -05:00
"sales_over_time": {
2014-01-28 11:46:26 -05:00
"buckets": [
{
2017-01-20 16:23:28 -05:00
"key_as_string": "2015-01-01",
"key": 1420070400000,
"doc_count": 3
2014-01-28 11:46:26 -05:00
},
{
2017-01-20 16:23:28 -05:00
"key_as_string": "2015-02-01",
"key": 1422748800000,
2014-01-28 11:46:26 -05:00
"doc_count": 2
},
2017-01-20 16:23:28 -05:00
{
"key_as_string": "2015-03-01",
"key": 1425168000000,
"doc_count": 2
}
2014-01-28 11:46:26 -05:00
]
}
2013-11-24 06:13:08 -05:00
}
}
--------------------------------------------------
2017-01-20 16:23:28 -05:00
// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
2013-11-24 06:13:08 -05:00
2015-09-07 13:53:50 -04:00
==== 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
2016-01-21 09:35:07 -05:00
`America/Los_Angeles`.
2015-09-07 13:53:50 -04:00
Consider the following example:
[source,js]
---------------------------------
2017-01-20 16:23:28 -05:00
PUT my_index/log/1?refresh
2015-09-07 13:53:50 -04:00
{
"date": "2015-10-01T00:30:00Z"
}
2017-01-20 16:23:28 -05:00
PUT my_index/log/2?refresh
2015-09-07 13:53:50 -04:00
{
"date": "2015-10-01T01:30:00Z"
}
GET my_index/_search?size=0
{
"aggs": {
"by_day": {
"date_histogram": {
"field": "date",
"interval": "day"
}
}
}
}
---------------------------------
2017-01-20 16:23:28 -05:00
// CONSOLE
2015-09-07 13:53:50 -04:00
UTC is used if no time zone is specified, which would result in both of these
documents being placed into the same day bucket, which starts at midnight UTC
on 1 October 2015:
[source,js]
---------------------------------
2017-01-20 16:23:28 -05:00
{
...
"aggregations": {
"by_day": {
"buckets": [
{
"key_as_string": "2015-10-01T00:00:00.000Z",
"key": 1443657600000,
"doc_count": 2
}
]
}
2015-09-07 13:53:50 -04:00
}
}
---------------------------------
2017-01-20 16:23:28 -05:00
// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
2015-09-07 13:53:50 -04:00
If a `time_zone` of `-01:00` is specified, then midnight starts at one hour before
midnight UTC:
[source,js]
---------------------------------
GET my_index/_search?size=0
{
"aggs": {
"by_day": {
"date_histogram": {
"field": "date",
"interval": "day",
"time_zone": "-01:00"
}
}
}
}
---------------------------------
2017-01-20 16:23:28 -05:00
// CONSOLE
// TEST[continued]
2015-09-07 13:53:50 -04:00
Now the first document falls into the bucket for 30 September 2015, while the
second document falls into the bucket for 1 October 2015:
[source,js]
---------------------------------
2017-01-20 16:23:28 -05:00
{
...
"aggregations": {
"by_day": {
"buckets": [
{
"key_as_string": "2015-09-30T00:00:00.000-01:00", <1>
"key": 1443574800000,
"doc_count": 1
},
{
"key_as_string": "2015-10-01T00:00:00.000-01:00", <1>
"key": 1443661200000,
"doc_count": 1
}
]
}
2015-09-07 13:53:50 -04:00
}
}
---------------------------------
2017-01-20 16:23:28 -05:00
// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
2015-09-07 13:53:50 -04:00
<1> The `key_as_string` value represents midnight on each day
in the specified time zone.
2016-11-15 13:06:35 -05:00
WARNING: 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 would be expected from the used `interval`.
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. When using `day` as `interval`,
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.
2015-09-07 13:53:50 -04:00
==== Offset
The `offset` parameter is used to change the start value of each bucket by the
specified positive (`+`) or negative offset (`-`) duration, such as `1h` for
an hour, or `1M` for a month. See <<time-units>> for more possible time
duration options.
For instance, when using an interval of `day`, each bucket runs from midnight
to midnight. Setting the `offset` parameter to `+6h` would change each bucket
to run from 6am to 6am:
[source,js]
-----------------------------
2017-01-20 16:23:28 -05:00
PUT my_index/log/1?refresh
2015-09-07 13:53:50 -04:00
{
"date": "2015-10-01T05:30:00Z"
}
2017-01-20 16:23:28 -05:00
PUT my_index/log/2?refresh
2015-09-07 13:53:50 -04:00
{
"date": "2015-10-01T06:30:00Z"
}
GET my_index/_search?size=0
{
"aggs": {
"by_day": {
"date_histogram": {
"field": "date",
"interval": "day",
"offset": "+6h"
}
}
}
}
-----------------------------
2017-01-20 16:23:28 -05:00
// CONSOLE
2015-09-07 13:53:50 -04:00
Instead of a single bucket starting at midnight, the above request groups the
documents into buckets starting at 6am:
[source,js]
-----------------------------
2017-01-20 16:23:28 -05:00
{
...
"aggregations": {
"by_day": {
"buckets": [
{
"key_as_string": "2015-09-30T06:00:00.000Z",
"key": 1443592800000,
"doc_count": 1
},
{
"key_as_string": "2015-10-01T06:00:00.000Z",
"key": 1443679200000,
"doc_count": 1
}
]
}
2015-09-07 13:53:50 -04:00
}
}
-----------------------------
2017-01-20 16:23:28 -05:00
// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
2015-09-07 13:53:50 -04:00
NOTE: The start `offset` of each bucket is calculated after the `time_zone`
adjustments have been made.
==== Scripts
2013-12-01 19:54:42 -05:00
Like with the normal <<search-aggregations-bucket-histogram-aggregation,histogram>>, both document level scripts and
Added extended_bounds support for date_/histogram aggs
By default the date_/histogram returns all the buckets within the range of the data itself, that is, the documents with the smallest values (on which with histogram) will determine the min bucket (the bucket with the smallest key) and the documents with the highest values will determine the max bucket (the bucket with the highest key). Often, when when requesting empty buckets (min_doc_count : 0), this causes a confusion, specifically, when the data is also filtered.
To understand why, let's look at an example:
Lets say the you're filtering your request to get all docs from the last month, and in the date_histogram aggs you'd like to slice the data per day. You also specify min_doc_count:0 so that you'd still get empty buckets for those days to which no document belongs. By default, if the first document that fall in this last month also happen to fall on the first day of the **second week** of the month, the date_histogram will **not** return empty buckets for all those days prior to that second week. The reason for that is that by default the histogram aggregations only start building buckets when they encounter documents (hence, missing on all the days of the first week in our example).
With extended_bounds, you now can "force" the histogram aggregations to start building buckets on a specific min values and also keep on building buckets up to a max value (even if there are no documents anymore). Using extended_bounds only makes sense when min_doc_count is 0 (the empty buckets will never be returned if the min_doc_count is greater than 0).
Note that (as the name suggest) extended_bounds is **not** filtering buckets. Meaning, if the min bounds is higher than the values extracted from the documents, the documents will still dictate what the min bucket will be (and the same goes to the extended_bounds.max and the max bucket). For filtering buckets, one should nest the histogram agg under a range filter agg with the appropriate min/max.
Closes #5224
2014-03-16 20:06:07 -04:00
value level scripts are supported. It is also possible to control the order of the returned buckets using the `order`
2015-04-30 08:55:34 -04:00
settings and filter the returned buckets based on a `min_doc_count` setting (by default all buckets between the first
bucket that matches documents and the last one are returned). This histogram also supports the `extended_bounds`
setting, which enables extending the bounds of the histogram beyond the data itself (to read more on why you'd want to
do that please refer to the explanation <<search-aggregations-bucket-histogram-aggregation-extended-bounds,here>>).
2015-05-07 10:46:40 -04:00
==== 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.
[source,js]
--------------------------------------------------
2017-01-20 16:23:28 -05:00
POST /sales/_search?size=0
2015-05-07 10:46:40 -04:00
{
"aggs" : {
2017-01-20 16:23:28 -05:00
"sale_date" : {
2015-10-01 07:49:09 -04:00
"date_histogram" : {
2017-01-20 16:23:28 -05:00
"field" : "date",
2015-05-07 10:46:40 -04:00
"interval": "year",
2017-01-20 16:23:28 -05:00
"missing": "2000/01/01" <1>
2015-05-07 10:46:40 -04:00
}
}
}
}
--------------------------------------------------
2017-01-20 16:23:28 -05:00
// CONSOLE
// TEST[setup:sales]
2015-05-07 10:46:40 -04:00
<1> Documents without a value in the `publish_date` field will fall into the same bucket as documents that have the value `2000-01-01`.