datehistogram-aggregation.asciidoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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` (`1y`), `quarter` (`1q`), `month` (`1M`), `week` (`1w`),
  27. `day` (`1d`), `hour` (`1h`), `minute` (`1m`), `second` (`1s`)
  28. Time values can also be specified via abbreviations supported by <<time-units,time units>> parsing.
  29. Note that fractional time values are not supported, but you can address this by shifting to another
  30. time unit (e.g., `1.5h` could instead be specified as `90m`). Also note that time intervals larger than
  31. days do not support arbitrary values but can only be one unit large (e.g. `1y` is valid, `2y` is not).
  32. [source,js]
  33. --------------------------------------------------
  34. POST /sales/_search?size=0
  35. {
  36. "aggs" : {
  37. "sales_over_time" : {
  38. "date_histogram" : {
  39. "field" : "date",
  40. "interval" : "90m"
  41. }
  42. }
  43. }
  44. }
  45. --------------------------------------------------
  46. // CONSOLE
  47. // TEST[setup:sales]
  48. ==== Keys
  49. Internally, a date is represented as a 64 bit number representing a timestamp
  50. in milliseconds-since-the-epoch. These timestamps are returned as the bucket
  51. ++key++s. The `key_as_string` is the same timestamp converted to a formatted
  52. date string using the format specified with the `format` parameter:
  53. TIP: If no `format` is specified, then it will use the first date
  54. <<mapping-date-format,format>> specified in the field mapping.
  55. [source,js]
  56. --------------------------------------------------
  57. POST /sales/_search?size=0
  58. {
  59. "aggs" : {
  60. "sales_over_time" : {
  61. "date_histogram" : {
  62. "field" : "date",
  63. "interval" : "1M",
  64. "format" : "yyyy-MM-dd" <1>
  65. }
  66. }
  67. }
  68. }
  69. --------------------------------------------------
  70. // CONSOLE
  71. // TEST[setup:sales]
  72. <1> Supports expressive date <<date-format-pattern,format pattern>>
  73. Response:
  74. [source,js]
  75. --------------------------------------------------
  76. {
  77. ...
  78. "aggregations": {
  79. "sales_over_time": {
  80. "buckets": [
  81. {
  82. "key_as_string": "2015-01-01",
  83. "key": 1420070400000,
  84. "doc_count": 3
  85. },
  86. {
  87. "key_as_string": "2015-02-01",
  88. "key": 1422748800000,
  89. "doc_count": 2
  90. },
  91. {
  92. "key_as_string": "2015-03-01",
  93. "key": 1425168000000,
  94. "doc_count": 2
  95. }
  96. ]
  97. }
  98. }
  99. }
  100. --------------------------------------------------
  101. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  102. ==== Time Zone
  103. Date-times are stored in Elasticsearch in UTC. By default, all bucketing and
  104. rounding is also done in UTC. The `time_zone` parameter can be used to indicate
  105. that bucketing should use a different time zone.
  106. Time zones may either be specified as an ISO 8601 UTC offset (e.g. `+01:00` or
  107. `-08:00`) or as a timezone id, an identifier used in the TZ database like
  108. `America/Los_Angeles`.
  109. Consider the following example:
  110. [source,js]
  111. ---------------------------------
  112. PUT my_index/_doc/1?refresh
  113. {
  114. "date": "2015-10-01T00:30:00Z"
  115. }
  116. PUT my_index/_doc/2?refresh
  117. {
  118. "date": "2015-10-01T01:30:00Z"
  119. }
  120. GET my_index/_search?size=0
  121. {
  122. "aggs": {
  123. "by_day": {
  124. "date_histogram": {
  125. "field": "date",
  126. "interval": "day"
  127. }
  128. }
  129. }
  130. }
  131. ---------------------------------
  132. // CONSOLE
  133. UTC is used if no time zone is specified, which would result in both of these
  134. documents being placed into the same day bucket, which starts at midnight UTC
  135. on 1 October 2015:
  136. [source,js]
  137. ---------------------------------
  138. {
  139. ...
  140. "aggregations": {
  141. "by_day": {
  142. "buckets": [
  143. {
  144. "key_as_string": "2015-10-01T00:00:00.000Z",
  145. "key": 1443657600000,
  146. "doc_count": 2
  147. }
  148. ]
  149. }
  150. }
  151. }
  152. ---------------------------------
  153. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  154. If a `time_zone` of `-01:00` is specified, then midnight starts at one hour before
  155. midnight UTC:
  156. [source,js]
  157. ---------------------------------
  158. GET my_index/_search?size=0
  159. {
  160. "aggs": {
  161. "by_day": {
  162. "date_histogram": {
  163. "field": "date",
  164. "interval": "day",
  165. "time_zone": "-01:00"
  166. }
  167. }
  168. }
  169. }
  170. ---------------------------------
  171. // CONSOLE
  172. // TEST[continued]
  173. Now the first document falls into the bucket for 30 September 2015, while the
  174. second document falls into the bucket for 1 October 2015:
  175. [source,js]
  176. ---------------------------------
  177. {
  178. ...
  179. "aggregations": {
  180. "by_day": {
  181. "buckets": [
  182. {
  183. "key_as_string": "2015-09-30T00:00:00.000-01:00", <1>
  184. "key": 1443574800000,
  185. "doc_count": 1
  186. },
  187. {
  188. "key_as_string": "2015-10-01T00:00:00.000-01:00", <1>
  189. "key": 1443661200000,
  190. "doc_count": 1
  191. }
  192. ]
  193. }
  194. }
  195. }
  196. ---------------------------------
  197. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  198. <1> The `key_as_string` value represents midnight on each day
  199. in the specified time zone.
  200. WARNING: When using time zones that follow DST (daylight savings time) changes,
  201. buckets close to the moment when those changes happen can have slightly different
  202. sizes than would be expected from the used `interval`.
  203. For example, consider a DST start in the `CET` time zone: on 27 March 2016 at 2am,
  204. clocks were turned forward 1 hour to 3am local time. When using `day` as `interval`,
  205. the bucket covering that day will only hold data for 23 hours instead of the usual
  206. 24 hours for other buckets. The same is true for shorter intervals like e.g. 12h.
  207. Here, we will have only a 11h bucket on the morning of 27 March when the DST shift
  208. happens.
  209. ==== Offset
  210. The `offset` parameter is used to change the start value of each bucket by the
  211. specified positive (`+`) or negative offset (`-`) duration, such as `1h` for
  212. an hour, or `1d` for a day. See <<time-units>> for more possible time
  213. duration options.
  214. For instance, when using an interval of `day`, each bucket runs from midnight
  215. to midnight. Setting the `offset` parameter to `+6h` would change each bucket
  216. to run from 6am to 6am:
  217. [source,js]
  218. -----------------------------
  219. PUT my_index/_doc/1?refresh
  220. {
  221. "date": "2015-10-01T05:30:00Z"
  222. }
  223. PUT my_index/_doc/2?refresh
  224. {
  225. "date": "2015-10-01T06:30:00Z"
  226. }
  227. GET my_index/_search?size=0
  228. {
  229. "aggs": {
  230. "by_day": {
  231. "date_histogram": {
  232. "field": "date",
  233. "interval": "day",
  234. "offset": "+6h"
  235. }
  236. }
  237. }
  238. }
  239. -----------------------------
  240. // CONSOLE
  241. Instead of a single bucket starting at midnight, the above request groups the
  242. documents into buckets starting at 6am:
  243. [source,js]
  244. -----------------------------
  245. {
  246. ...
  247. "aggregations": {
  248. "by_day": {
  249. "buckets": [
  250. {
  251. "key_as_string": "2015-09-30T06:00:00.000Z",
  252. "key": 1443592800000,
  253. "doc_count": 1
  254. },
  255. {
  256. "key_as_string": "2015-10-01T06:00:00.000Z",
  257. "key": 1443679200000,
  258. "doc_count": 1
  259. }
  260. ]
  261. }
  262. }
  263. }
  264. -----------------------------
  265. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  266. NOTE: The start `offset` of each bucket is calculated after the `time_zone`
  267. adjustments have been made.
  268. ==== Keyed Response
  269. Setting the `keyed` flag to `true` will associate a unique string key with each bucket and return the ranges as a hash rather than an array:
  270. [source,js]
  271. --------------------------------------------------
  272. POST /sales/_search?size=0
  273. {
  274. "aggs" : {
  275. "sales_over_time" : {
  276. "date_histogram" : {
  277. "field" : "date",
  278. "interval" : "1M",
  279. "format" : "yyyy-MM-dd",
  280. "keyed": true
  281. }
  282. }
  283. }
  284. }
  285. --------------------------------------------------
  286. // CONSOLE
  287. // TEST[setup:sales]
  288. Response:
  289. [source,js]
  290. --------------------------------------------------
  291. {
  292. ...
  293. "aggregations": {
  294. "sales_over_time": {
  295. "buckets": {
  296. "2015-01-01": {
  297. "key_as_string": "2015-01-01",
  298. "key": 1420070400000,
  299. "doc_count": 3
  300. },
  301. "2015-02-01": {
  302. "key_as_string": "2015-02-01",
  303. "key": 1422748800000,
  304. "doc_count": 2
  305. },
  306. "2015-03-01": {
  307. "key_as_string": "2015-03-01",
  308. "key": 1425168000000,
  309. "doc_count": 2
  310. }
  311. }
  312. }
  313. }
  314. }
  315. --------------------------------------------------
  316. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  317. ==== Scripts
  318. Like with the normal <<search-aggregations-bucket-histogram-aggregation,histogram>>, both document level scripts and
  319. value level scripts are supported. It is also possible to control the order of the returned buckets using the `order`
  320. settings and filter the returned buckets based on a `min_doc_count` setting (by default all buckets between the first
  321. bucket that matches documents and the last one are returned). This histogram also supports the `extended_bounds`
  322. setting, which enables extending the bounds of the histogram beyond the data itself (to read more on why you'd want to
  323. do that please refer to the explanation <<search-aggregations-bucket-histogram-aggregation-extended-bounds,here>>).
  324. ==== Missing value
  325. The `missing` parameter defines how documents that are missing a value should be treated.
  326. By default they will be ignored but it is also possible to treat them as if they
  327. had a value.
  328. [source,js]
  329. --------------------------------------------------
  330. POST /sales/_search?size=0
  331. {
  332. "aggs" : {
  333. "sale_date" : {
  334. "date_histogram" : {
  335. "field" : "date",
  336. "interval": "year",
  337. "missing": "2000/01/01" <1>
  338. }
  339. }
  340. }
  341. }
  342. --------------------------------------------------
  343. // CONSOLE
  344. // TEST[setup:sales]
  345. <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`.
  346. ==== Order
  347. By default the returned buckets are sorted by their `key` ascending, though the order behaviour can be controlled using
  348. the `order` setting. Supports the same `order` functionality as the <<search-aggregations-bucket-terms-aggregation-order,`Terms Aggregation`>>.
  349. deprecated[6.0.0, Use `_key` instead of `_time` to order buckets by their dates/keys]
  350. ==== Use of a script to aggregate by day of the week
  351. There are some cases where date histogram can't help us, like for example, when we need
  352. to aggregate the results by day of the week.
  353. In this case to overcome the problem, we can use a script that returns the day of the week:
  354. [source,js]
  355. --------------------------------------------------
  356. POST /sales/_search?size=0
  357. {
  358. "aggs": {
  359. "dayOfWeek": {
  360. "terms": {
  361. "script": {
  362. "lang": "painless",
  363. "source": "doc['date'].value.dayOfWeekEnum.value"
  364. }
  365. }
  366. }
  367. }
  368. }
  369. --------------------------------------------------
  370. // CONSOLE
  371. // TEST[setup:sales]
  372. Response:
  373. [source,js]
  374. --------------------------------------------------
  375. {
  376. ...
  377. "aggregations": {
  378. "dayOfWeek": {
  379. "doc_count_error_upper_bound": 0,
  380. "sum_other_doc_count": 0,
  381. "buckets": [
  382. {
  383. "key": "7",
  384. "doc_count": 4
  385. },
  386. {
  387. "key": "4",
  388. "doc_count": 3
  389. }
  390. ]
  391. }
  392. }
  393. }
  394. --------------------------------------------------
  395. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  396. The response will contain all the buckets having as key the relative day of
  397. the week: 1 for Monday, 2 for Tuesday... 7 for Sunday.