autodatehistogram-aggregation.asciidoc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. [[search-aggregations-bucket-autodatehistogram-aggregation]]
  2. === Auto-interval Date Histogram Aggregation
  3. A multi-bucket aggregation similar to the <<search-aggregations-bucket-datehistogram-aggregation>> except
  4. instead of providing an interval to use as the width of each bucket, a target number of buckets is provided
  5. indicating the number of buckets needed and the interval of the buckets is automatically chosen to best achieve
  6. that target. The number of buckets returned will always be less than or equal to this target number.
  7. The buckets field is optional, and will default to 10 buckets if not specified.
  8. Requesting a target of 10 buckets.
  9. [source,js]
  10. --------------------------------------------------
  11. POST /sales/_search?size=0
  12. {
  13. "aggs" : {
  14. "sales_over_time" : {
  15. "auto_date_histogram" : {
  16. "field" : "date",
  17. "buckets" : 10
  18. }
  19. }
  20. }
  21. }
  22. --------------------------------------------------
  23. // CONSOLE
  24. // TEST[setup:sales]
  25. ==== Keys
  26. Internally, a date is represented as a 64 bit number representing a timestamp
  27. in milliseconds-since-the-epoch. These timestamps are returned as the bucket
  28. ++key++s. The `key_as_string` is the same timestamp converted to a formatted
  29. date string using the format specified with the `format` parameter:
  30. TIP: If no `format` is specified, then it will use the first date
  31. <<mapping-date-format,format>> specified in the field mapping.
  32. [source,js]
  33. --------------------------------------------------
  34. POST /sales/_search?size=0
  35. {
  36. "aggs" : {
  37. "sales_over_time" : {
  38. "auto_date_histogram" : {
  39. "field" : "date",
  40. "buckets" : 5,
  41. "format" : "yyyy-MM-dd" <1>
  42. }
  43. }
  44. }
  45. }
  46. --------------------------------------------------
  47. // CONSOLE
  48. // TEST[setup:sales]
  49. <1> Supports expressive date <<date-format-pattern,format pattern>>
  50. Response:
  51. [source,js]
  52. --------------------------------------------------
  53. {
  54. ...
  55. "aggregations": {
  56. "sales_over_time": {
  57. "buckets": [
  58. {
  59. "key_as_string": "2015-01-01",
  60. "key": 1420070400000,
  61. "doc_count": 3
  62. },
  63. {
  64. "key_as_string": "2015-02-01",
  65. "key": 1422748800000,
  66. "doc_count": 2
  67. },
  68. {
  69. "key_as_string": "2015-03-01",
  70. "key": 1425168000000,
  71. "doc_count": 2
  72. }
  73. ],
  74. "interval": "1M"
  75. }
  76. }
  77. }
  78. --------------------------------------------------
  79. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  80. === Intervals
  81. The interval of the returned buckets is selected based on the data collected by the
  82. aggregation so that the number of buckets returned is less than or equal to the number
  83. requested. The possible intervals returned are:
  84. [horizontal]
  85. seconds:: In multiples of 1, 5, 10 and 30
  86. minutes:: In multiples of 1, 5, 10 and 30
  87. hours:: In multiples of 1, 3 and 12
  88. days:: In multiples of 1, and 7
  89. months:: In multiples of 1, and 3
  90. years:: In multiples of 1, 5, 10, 20, 50 and 100
  91. In the worst case, where the number of daily buckets are too many for the requested
  92. number of buckets, the number of buckets returned will be 1/7th of the number of
  93. buckets requested.
  94. ==== Time Zone
  95. Date-times are stored in Elasticsearch in UTC. By default, all bucketing and
  96. rounding is also done in UTC. The `time_zone` parameter can be used to indicate
  97. that bucketing should use a different time zone.
  98. Time zones may either be specified as an ISO 8601 UTC offset (e.g. `+01:00` or
  99. `-08:00`) or as a timezone id, an identifier used in the TZ database like
  100. `America/Los_Angeles`.
  101. Consider the following example:
  102. [source,js]
  103. ---------------------------------
  104. PUT my_index/log/1?refresh
  105. {
  106. "date": "2015-10-01T00:30:00Z"
  107. }
  108. PUT my_index/log/2?refresh
  109. {
  110. "date": "2015-10-01T01:30:00Z"
  111. }
  112. PUT my_index/log/3?refresh
  113. {
  114. "date": "2015-10-01T02:30:00Z"
  115. }
  116. GET my_index/_search?size=0
  117. {
  118. "aggs": {
  119. "by_day": {
  120. "auto_date_histogram": {
  121. "field": "date",
  122. "buckets" : 3
  123. }
  124. }
  125. }
  126. }
  127. ---------------------------------
  128. // CONSOLE
  129. UTC is used if no time zone is specified, three 1-hour buckets are returned
  130. starting at midnight UTC on 1 October 2015:
  131. [source,js]
  132. ---------------------------------
  133. {
  134. ...
  135. "aggregations": {
  136. "by_day": {
  137. "buckets": [
  138. {
  139. "key_as_string": "2015-10-01T00:00:00.000Z",
  140. "key": 1443657600000,
  141. "doc_count": 1
  142. },
  143. {
  144. "key_as_string": "2015-10-01T01:00:00.000Z",
  145. "key": 1443661200000,
  146. "doc_count": 1
  147. },
  148. {
  149. "key_as_string": "2015-10-01T02:00:00.000Z",
  150. "key": 1443664800000,
  151. "doc_count": 1
  152. }
  153. ],
  154. "interval": "1h"
  155. }
  156. }
  157. }
  158. ---------------------------------
  159. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  160. If a `time_zone` of `-01:00` is specified, then midnight starts at one hour before
  161. midnight UTC:
  162. [source,js]
  163. ---------------------------------
  164. GET my_index/_search?size=0
  165. {
  166. "aggs": {
  167. "by_day": {
  168. "auto_date_histogram": {
  169. "field": "date",
  170. "buckets" : 3,
  171. "time_zone": "-01:00"
  172. }
  173. }
  174. }
  175. }
  176. ---------------------------------
  177. // CONSOLE
  178. // TEST[continued]
  179. Now three 1-hour buckets are still returned but the first bucket starts at
  180. 11:00pm on 30 September 2015 since that is the local time for the bucket in
  181. the specified time zone.
  182. [source,js]
  183. ---------------------------------
  184. {
  185. ...
  186. "aggregations": {
  187. "by_day": {
  188. "buckets": [
  189. {
  190. "key_as_string": "2015-09-30T23:00:00.000-01:00", <1>
  191. "key": 1443657600000,
  192. "doc_count": 1
  193. },
  194. {
  195. "key_as_string": "2015-10-01T00:00:00.000-01:00",
  196. "key": 1443661200000,
  197. "doc_count": 1
  198. },
  199. {
  200. "key_as_string": "2015-10-01T01:00:00.000-01:00",
  201. "key": 1443664800000,
  202. "doc_count": 1
  203. }
  204. ],
  205. "interval": "1h"
  206. }
  207. }
  208. }
  209. ---------------------------------
  210. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  211. <1> The `key_as_string` value represents midnight on each day
  212. in the specified time zone.
  213. WARNING: When using time zones that follow DST (daylight savings time) changes,
  214. buckets close to the moment when those changes happen can have slightly different
  215. sizes than neighbouring buckets.
  216. For example, consider a DST start in the `CET` time zone: on 27 March 2016 at 2am,
  217. clocks were turned forward 1 hour to 3am local time. If the result of the aggregation
  218. was daily buckets, the bucket covering that day will only hold data for 23 hours
  219. instead of the usual 24 hours for other buckets. The same is true for shorter intervals
  220. like e.g. 12h. Here, we will have only a 11h bucket on the morning of 27 March when the
  221. DST shift happens.
  222. ==== Scripts
  223. Like with the normal <<search-aggregations-bucket-datehistogram-aggregation, `date_histogram`>>, both document level
  224. scripts and value level scripts are supported. This aggregation does not however, support the `min_doc_count`,
  225. `extended_bounds` and `order` parameters.
  226. ==== Missing value
  227. The `missing` parameter defines how documents that are missing a value should be treated.
  228. By default they will be ignored but it is also possible to treat them as if they
  229. had a value.
  230. [source,js]
  231. --------------------------------------------------
  232. POST /sales/_search?size=0
  233. {
  234. "aggs" : {
  235. "sale_date" : {
  236. "auto_date_histogram" : {
  237. "field" : "date",
  238. "buckets": 10,
  239. "missing": "2000/01/01" <1>
  240. }
  241. }
  242. }
  243. }
  244. --------------------------------------------------
  245. // CONSOLE
  246. // TEST[setup:sales]
  247. <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`.