datehistogram-aggregation.asciidoc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. [[search-aggregations-bucket-datehistogram-aggregation]]
  2. === Date Histogram Aggregation
  3. A multi-bucket aggregation similar to the <<search-aggregations-bucket-histogram-aggregation,histogram>> except it can
  4. only be applied on date values. Since dates are represented in elasticsearch internally as long values, it is possible
  5. to use the normal `histogram` on dates as well, though accuracy will be compromised. The reason for this is in the fact
  6. that time based intervals are not fixed (think of leap years and on the number of days in a month). For this reason,
  7. we need special support for time based data. From a functionality perspective, this histogram supports the same features
  8. as the normal <<search-aggregations-bucket-histogram-aggregation,histogram>>. The main difference is that the interval can be specified by date/time expressions.
  9. Requesting bucket intervals of a month.
  10. [source,js]
  11. --------------------------------------------------
  12. POST /sales/_search?size=0
  13. {
  14. "aggs" : {
  15. "sales_over_time" : {
  16. "date_histogram" : {
  17. "field" : "date",
  18. "interval" : "month"
  19. }
  20. }
  21. }
  22. }
  23. --------------------------------------------------
  24. // CONSOLE
  25. // TEST[setup:sales]
  26. Available expressions for interval: `year`, `quarter`, `month`, `week`, `day`, `hour`, `minute`, `second`
  27. Time values can also be specified via abbreviations supported by <<time-units,time units>> parsing.
  28. Note that fractional time values are not supported, but you can address this by shifting to another
  29. time unit (e.g., `1.5h` could instead be specified as `90m`).
  30. [source,js]
  31. --------------------------------------------------
  32. POST /sales/_search?size=0
  33. {
  34. "aggs" : {
  35. "sales_over_time" : {
  36. "date_histogram" : {
  37. "field" : "date",
  38. "interval" : "90m"
  39. }
  40. }
  41. }
  42. }
  43. --------------------------------------------------
  44. // CONSOLE
  45. // TEST[setup:sales]
  46. ==== Keys
  47. Internally, a date is represented as a 64 bit number representing a timestamp
  48. in milliseconds-since-the-epoch. These timestamps are returned as the bucket
  49. ++key++s. The `key_as_string` is the same timestamp converted to a formatted
  50. date string using the format specified with the `format` parameter:
  51. TIP: If no `format` is specified, then it will use the first date
  52. <<mapping-date-format,format>> specified in the field mapping.
  53. [source,js]
  54. --------------------------------------------------
  55. POST /sales/_search?size=0
  56. {
  57. "aggs" : {
  58. "sales_over_time" : {
  59. "date_histogram" : {
  60. "field" : "date",
  61. "interval" : "1M",
  62. "format" : "yyyy-MM-dd" <1>
  63. }
  64. }
  65. }
  66. }
  67. --------------------------------------------------
  68. // CONSOLE
  69. // TEST[setup:sales]
  70. <1> Supports expressive date <<date-format-pattern,format pattern>>
  71. Response:
  72. [source,js]
  73. --------------------------------------------------
  74. {
  75. ...
  76. "aggregations": {
  77. "sales_over_time": {
  78. "buckets": [
  79. {
  80. "key_as_string": "2015-01-01",
  81. "key": 1420070400000,
  82. "doc_count": 3
  83. },
  84. {
  85. "key_as_string": "2015-02-01",
  86. "key": 1422748800000,
  87. "doc_count": 2
  88. },
  89. {
  90. "key_as_string": "2015-03-01",
  91. "key": 1425168000000,
  92. "doc_count": 2
  93. }
  94. ]
  95. }
  96. }
  97. }
  98. --------------------------------------------------
  99. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  100. ==== Time Zone
  101. Date-times are stored in Elasticsearch in UTC. By default, all bucketing and
  102. rounding is also done in UTC. The `time_zone` parameter can be used to indicate
  103. that bucketing should use a different time zone.
  104. Time zones may either be specified as an ISO 8601 UTC offset (e.g. `+01:00` or
  105. `-08:00`) or as a timezone id, an identifier used in the TZ database like
  106. `America/Los_Angeles`.
  107. Consider the following example:
  108. [source,js]
  109. ---------------------------------
  110. PUT my_index/log/1?refresh
  111. {
  112. "date": "2015-10-01T00:30:00Z"
  113. }
  114. PUT my_index/log/2?refresh
  115. {
  116. "date": "2015-10-01T01:30:00Z"
  117. }
  118. GET my_index/_search?size=0
  119. {
  120. "aggs": {
  121. "by_day": {
  122. "date_histogram": {
  123. "field": "date",
  124. "interval": "day"
  125. }
  126. }
  127. }
  128. }
  129. ---------------------------------
  130. // CONSOLE
  131. UTC is used if no time zone is specified, which would result in both of these
  132. documents being placed into the same day bucket, which starts at midnight UTC
  133. on 1 October 2015:
  134. [source,js]
  135. ---------------------------------
  136. {
  137. ...
  138. "aggregations": {
  139. "by_day": {
  140. "buckets": [
  141. {
  142. "key_as_string": "2015-10-01T00:00:00.000Z",
  143. "key": 1443657600000,
  144. "doc_count": 2
  145. }
  146. ]
  147. }
  148. }
  149. }
  150. ---------------------------------
  151. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  152. If a `time_zone` of `-01:00` is specified, then midnight starts at one hour before
  153. midnight UTC:
  154. [source,js]
  155. ---------------------------------
  156. GET my_index/_search?size=0
  157. {
  158. "aggs": {
  159. "by_day": {
  160. "date_histogram": {
  161. "field": "date",
  162. "interval": "day",
  163. "time_zone": "-01:00"
  164. }
  165. }
  166. }
  167. }
  168. ---------------------------------
  169. // CONSOLE
  170. // TEST[continued]
  171. Now the first document falls into the bucket for 30 September 2015, while the
  172. second document falls into the bucket for 1 October 2015:
  173. [source,js]
  174. ---------------------------------
  175. {
  176. ...
  177. "aggregations": {
  178. "by_day": {
  179. "buckets": [
  180. {
  181. "key_as_string": "2015-09-30T00:00:00.000-01:00", <1>
  182. "key": 1443574800000,
  183. "doc_count": 1
  184. },
  185. {
  186. "key_as_string": "2015-10-01T00:00:00.000-01:00", <1>
  187. "key": 1443661200000,
  188. "doc_count": 1
  189. }
  190. ]
  191. }
  192. }
  193. }
  194. ---------------------------------
  195. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  196. <1> The `key_as_string` value represents midnight on each day
  197. in the specified time zone.
  198. WARNING: When using time zones that follow DST (daylight savings time) changes,
  199. buckets close to the moment when those changes happen can have slightly different
  200. sizes than would be expected from the used `interval`.
  201. For example, consider a DST start in the `CET` time zone: on 27 March 2016 at 2am,
  202. clocks were turned forward 1 hour to 3am local time. When using `day` as `interval`,
  203. the bucket covering that day will only hold data for 23 hours instead of the usual
  204. 24 hours for other buckets. The same is true for shorter intervals like e.g. 12h.
  205. Here, we will have only a 11h bucket on the morning of 27 March when the DST shift
  206. happens.
  207. ==== Offset
  208. The `offset` parameter is used to change the start value of each bucket by the
  209. specified positive (`+`) or negative offset (`-`) duration, such as `1h` for
  210. an hour, or `1M` for a month. See <<time-units>> for more possible time
  211. duration options.
  212. For instance, when using an interval of `day`, each bucket runs from midnight
  213. to midnight. Setting the `offset` parameter to `+6h` would change each bucket
  214. to run from 6am to 6am:
  215. [source,js]
  216. -----------------------------
  217. PUT my_index/log/1?refresh
  218. {
  219. "date": "2015-10-01T05:30:00Z"
  220. }
  221. PUT my_index/log/2?refresh
  222. {
  223. "date": "2015-10-01T06:30:00Z"
  224. }
  225. GET my_index/_search?size=0
  226. {
  227. "aggs": {
  228. "by_day": {
  229. "date_histogram": {
  230. "field": "date",
  231. "interval": "day",
  232. "offset": "+6h"
  233. }
  234. }
  235. }
  236. }
  237. -----------------------------
  238. // CONSOLE
  239. Instead of a single bucket starting at midnight, the above request groups the
  240. documents into buckets starting at 6am:
  241. [source,js]
  242. -----------------------------
  243. {
  244. ...
  245. "aggregations": {
  246. "by_day": {
  247. "buckets": [
  248. {
  249. "key_as_string": "2015-09-30T06:00:00.000Z",
  250. "key": 1443592800000,
  251. "doc_count": 1
  252. },
  253. {
  254. "key_as_string": "2015-10-01T06:00:00.000Z",
  255. "key": 1443679200000,
  256. "doc_count": 1
  257. }
  258. ]
  259. }
  260. }
  261. }
  262. -----------------------------
  263. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  264. NOTE: The start `offset` of each bucket is calculated after the `time_zone`
  265. adjustments have been made.
  266. ==== Scripts
  267. Like with the normal <<search-aggregations-bucket-histogram-aggregation,histogram>>, both document level scripts and
  268. value level scripts are supported. It is also possible to control the order of the returned buckets using the `order`
  269. settings and filter the returned buckets based on a `min_doc_count` setting (by default all buckets between the first
  270. bucket that matches documents and the last one are returned). This histogram also supports the `extended_bounds`
  271. setting, which enables extending the bounds of the histogram beyond the data itself (to read more on why you'd want to
  272. do that please refer to the explanation <<search-aggregations-bucket-histogram-aggregation-extended-bounds,here>>).
  273. ==== Missing value
  274. The `missing` parameter defines how documents that are missing a value should be treated.
  275. By default they will be ignored but it is also possible to treat them as if they
  276. had a value.
  277. [source,js]
  278. --------------------------------------------------
  279. POST /sales/_search?size=0
  280. {
  281. "aggs" : {
  282. "sale_date" : {
  283. "date_histogram" : {
  284. "field" : "date",
  285. "interval": "year",
  286. "missing": "2000/01/01" <1>
  287. }
  288. }
  289. }
  290. }
  291. --------------------------------------------------
  292. // CONSOLE
  293. // TEST[setup:sales]
  294. <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`.