datehistogram-aggregation.asciidoc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. [[search-aggregations-bucket-datehistogram-aggregation]]
  2. === Date histogram aggregation
  3. ++++
  4. <titleabbrev>Date histogram</titleabbrev>
  5. ++++
  6. This multi-bucket aggregation is similar to the normal
  7. <<search-aggregations-bucket-histogram-aggregation,histogram>>, but it can
  8. only be used with date or date range values. Because dates are represented internally in
  9. Elasticsearch as long values, it is possible, but not as accurate, to use the
  10. normal `histogram` on dates as well. The main difference in the two APIs is
  11. that here the interval can be specified using date/time expressions. Time-based
  12. data requires special support because time-based intervals are not always a
  13. fixed length.
  14. Like the histogram, values are rounded *down* into the closest bucket. For
  15. example, if the interval is a calendar day, `2020-01-03T07:00:01Z` is rounded to
  16. `2020-01-03T00:00:00Z`. Values are rounded as follows:
  17. [source,java]
  18. ----
  19. bucket_key = Math.floor(value / interval) * interval
  20. ----
  21. [[calendar_and_fixed_intervals]]
  22. ==== Calendar and fixed intervals
  23. When configuring a date histogram aggregation, the interval can be specified
  24. in two manners: calendar-aware time intervals, and fixed time intervals.
  25. Calendar-aware intervals understand that daylight savings changes the length
  26. of specific days, months have different amounts of days, and leap seconds can
  27. be tacked onto a particular year.
  28. Fixed intervals are, by contrast, always multiples of SI units and do not change
  29. based on calendaring context.
  30. [NOTE]
  31. .Combined `interval` field is deprecated
  32. ==================================
  33. deprecated[7.2, `interval` field is deprecated] Historically both calendar and fixed
  34. intervals were configured in a single `interval` field, which led to confusing
  35. semantics. Specifying `1d` would be assumed as a calendar-aware time,
  36. whereas `2d` would be interpreted as fixed time. To get "one day" of fixed time,
  37. the user would need to specify the next smaller unit (in this case, `24h`).
  38. This combined behavior was often unknown to users, and even when knowledgeable about
  39. the behavior it was difficult to use and confusing.
  40. This behavior has been deprecated in favor of two new, explicit fields: `calendar_interval`
  41. and `fixed_interval`.
  42. By forcing a choice between calendar and intervals up front, the semantics of the interval
  43. are clear to the user immediately and there is no ambiguity. The old `interval` field
  44. will be removed in the future.
  45. ==================================
  46. [[calendar_intervals]]
  47. ==== Calendar intervals
  48. Calendar-aware intervals are configured with the `calendar_interval` parameter.
  49. You can specify calendar intervals using the unit name, such as `month`, or as a
  50. single unit quantity, such as `1M`. For example, `day` and `1d` are equivalent.
  51. Multiple quantities, such as `2d`, are not supported.
  52. The accepted calendar intervals are:
  53. `minute`, `1m` ::
  54. All minutes begin at 00 seconds.
  55. One minute is the interval between 00 seconds of the first minute and 00
  56. seconds of the following minute in the specified time zone, compensating for any
  57. intervening leap seconds, so that the number of minutes and seconds past the
  58. hour is the same at the start and end.
  59. `hour`, `1h` ::
  60. All hours begin at 00 minutes and 00 seconds.
  61. One hour (1h) is the interval between 00:00 minutes of the first hour and 00:00
  62. minutes of the following hour in the specified time zone, compensating for any
  63. intervening leap seconds, so that the number of minutes and seconds past the hour
  64. is the same at the start and end.
  65. `day`, `1d` ::
  66. All days begin at the earliest possible time, which is usually 00:00:00
  67. (midnight).
  68. One day (1d) is the interval between the start of the day and the start of
  69. the following day in the specified time zone, compensating for any intervening
  70. time changes.
  71. `week`, `1w` ::
  72. One week is the interval between the start day_of_week:hour:minute:second
  73. and the same day of the week and time of the following week in the specified
  74. time zone.
  75. `month`, `1M` ::
  76. One month is the interval between the start day of the month and time of
  77. day and the same day of the month and time of the following month in the specified
  78. time zone, so that the day of the month and time of day are the same at the start
  79. and end.
  80. `quarter`, `1q` ::
  81. One quarter is the interval between the start day of the month and
  82. time of day and the same day of the month and time of day three months later,
  83. so that the day of the month and time of day are the same at the start and end. +
  84. `year`, `1y` ::
  85. One year is the interval between the start day of the month and time of
  86. day and the same day of the month and time of day the following year in the
  87. specified time zone, so that the date and time are the same at the start and end. +
  88. [[calendar_interval_examples]]
  89. ===== Calendar interval examples
  90. As an example, here is an aggregation requesting bucket intervals of a month in calendar time:
  91. [source,console,id=datehistogram-aggregation-calendar-interval-example]
  92. --------------------------------------------------
  93. POST /sales/_search?size=0
  94. {
  95. "aggs": {
  96. "sales_over_time": {
  97. "date_histogram": {
  98. "field": "date",
  99. "calendar_interval": "month"
  100. }
  101. }
  102. }
  103. }
  104. --------------------------------------------------
  105. // TEST[setup:sales]
  106. If you attempt to use multiples of calendar units, the aggregation will fail because only
  107. singular calendar units are supported:
  108. [source,console,id=datehistogram-aggregation-calendar-interval-multiples-example]
  109. --------------------------------------------------
  110. POST /sales/_search?size=0
  111. {
  112. "aggs": {
  113. "sales_over_time": {
  114. "date_histogram": {
  115. "field": "date",
  116. "calendar_interval": "2d"
  117. }
  118. }
  119. }
  120. }
  121. --------------------------------------------------
  122. // TEST[setup:sales]
  123. // TEST[catch:bad_request]
  124. [source,js]
  125. --------------------------------------------------
  126. {
  127. "error" : {
  128. "root_cause" : [...],
  129. "type" : "x_content_parse_exception",
  130. "reason" : "[1:82] [date_histogram] failed to parse field [calendar_interval]",
  131. "caused_by" : {
  132. "type" : "illegal_argument_exception",
  133. "reason" : "The supplied interval [2d] could not be parsed as a calendar interval.",
  134. "stack_trace" : "java.lang.IllegalArgumentException: The supplied interval [2d] could not be parsed as a calendar interval."
  135. }
  136. }
  137. }
  138. --------------------------------------------------
  139. // NOTCONSOLE
  140. [[fixed_intervals]]
  141. ==== Fixed intervals
  142. Fixed intervals are configured with the `fixed_interval` parameter.
  143. In contrast to calendar-aware intervals, fixed intervals are a fixed number of SI
  144. units and never deviate, regardless of where they fall on the calendar. One second
  145. is always composed of `1000ms`. This allows fixed intervals to be specified in
  146. any multiple of the supported units.
  147. However, it means fixed intervals cannot express other units such as months,
  148. since the duration of a month is not a fixed quantity. Attempting to specify
  149. a calendar interval like month or quarter will throw an exception.
  150. The accepted units for fixed intervals are:
  151. milliseconds (`ms`) ::
  152. A single millisecond. This is a very, very small interval.
  153. seconds (`s`) ::
  154. Defined as 1000 milliseconds each.
  155. minutes (`m`) ::
  156. Defined as 60 seconds each (60,000 milliseconds).
  157. All minutes begin at 00 seconds.
  158. hours (`h`) ::
  159. Defined as 60 minutes each (3,600,000 milliseconds).
  160. All hours begin at 00 minutes and 00 seconds.
  161. days (`d`) ::
  162. Defined as 24 hours (86,400,000 milliseconds).
  163. All days begin at the earliest possible time, which is usually 00:00:00
  164. (midnight).
  165. [[fixed_interval_examples]]
  166. ===== Fixed interval examples
  167. If we try to recreate the "month" `calendar_interval` from earlier, we can approximate that with
  168. 30 fixed days:
  169. [source,console,id=datehistogram-aggregation-fixed-interval-example]
  170. --------------------------------------------------
  171. POST /sales/_search?size=0
  172. {
  173. "aggs": {
  174. "sales_over_time": {
  175. "date_histogram": {
  176. "field": "date",
  177. "fixed_interval": "30d"
  178. }
  179. }
  180. }
  181. }
  182. --------------------------------------------------
  183. // TEST[setup:sales]
  184. But if we try to use a calendar unit that is not supported, such as weeks, we'll get an exception:
  185. [source,console,id=datehistogram-aggregation-fixed-interval-unsupported-example]
  186. --------------------------------------------------
  187. POST /sales/_search?size=0
  188. {
  189. "aggs": {
  190. "sales_over_time": {
  191. "date_histogram": {
  192. "field": "date",
  193. "fixed_interval": "2w"
  194. }
  195. }
  196. }
  197. }
  198. --------------------------------------------------
  199. // TEST[setup:sales]
  200. // TEST[catch:bad_request]
  201. [source,js]
  202. --------------------------------------------------
  203. {
  204. "error" : {
  205. "root_cause" : [...],
  206. "type" : "x_content_parse_exception",
  207. "reason" : "[1:82] [date_histogram] failed to parse field [fixed_interval]",
  208. "caused_by" : {
  209. "type" : "illegal_argument_exception",
  210. "reason" : "failed to parse setting [date_histogram.fixedInterval] with value [2w] as a time value: unit is missing or unrecognized",
  211. "stack_trace" : "java.lang.IllegalArgumentException: failed to parse setting [date_histogram.fixedInterval] with value [2w] as a time value: unit is missing or unrecognized"
  212. }
  213. }
  214. }
  215. --------------------------------------------------
  216. // NOTCONSOLE
  217. [[datehistogram-aggregation-notes]]
  218. ==== Date histogram usage notes
  219. In all cases, when the specified end time does not exist, the actual end time is
  220. the closest available time after the specified end.
  221. Widely distributed applications must also consider vagaries such as countries that
  222. start and stop daylight savings time at 12:01 A.M., so end up with one minute of
  223. Sunday followed by an additional 59 minutes of Saturday once a year, and countries
  224. that decide to move across the international date line. Situations like
  225. that can make irregular time zone offsets seem easy.
  226. As always, rigorous testing, especially around time-change events, will ensure
  227. that your time interval specification is
  228. what you intend it to be.
  229. WARNING: To avoid unexpected results, all connected servers and clients must
  230. sync to a reliable network time service.
  231. NOTE: Fractional time values are not supported, but you can address this by
  232. shifting to another time unit (e.g., `1.5h` could instead be specified as `90m`).
  233. NOTE: You can also specify time values using abbreviations supported by
  234. <<time-units,time units>> parsing.
  235. [[datehistogram-aggregation-keys]]
  236. ==== Keys
  237. Internally, a date is represented as a 64 bit number representing a timestamp
  238. in milliseconds-since-the-epoch (01/01/1970 midnight UTC). These timestamps are
  239. returned as the ++key++ name of the bucket. The `key_as_string` is the same
  240. timestamp converted to a formatted
  241. date string using the `format` parameter specification:
  242. TIP: If you don't specify `format`, the first date
  243. <<mapping-date-format,format>> specified in the field mapping is used.
  244. [source,console,id=datehistogram-aggregation-format-example]
  245. --------------------------------------------------
  246. POST /sales/_search?size=0
  247. {
  248. "aggs": {
  249. "sales_over_time": {
  250. "date_histogram": {
  251. "field": "date",
  252. "calendar_interval": "1M",
  253. "format": "yyyy-MM-dd" <1>
  254. }
  255. }
  256. }
  257. }
  258. --------------------------------------------------
  259. // TEST[setup:sales]
  260. <1> Supports expressive date <<date-format-pattern,format pattern>>
  261. Response:
  262. [source,console-result]
  263. --------------------------------------------------
  264. {
  265. ...
  266. "aggregations": {
  267. "sales_over_time": {
  268. "buckets": [
  269. {
  270. "key_as_string": "2015-01-01",
  271. "key": 1420070400000,
  272. "doc_count": 3
  273. },
  274. {
  275. "key_as_string": "2015-02-01",
  276. "key": 1422748800000,
  277. "doc_count": 2
  278. },
  279. {
  280. "key_as_string": "2015-03-01",
  281. "key": 1425168000000,
  282. "doc_count": 2
  283. }
  284. ]
  285. }
  286. }
  287. }
  288. --------------------------------------------------
  289. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  290. [[datehistogram-aggregation-time-zone]]
  291. ==== Time zone
  292. {es} stores date-times in Coordinated Universal Time (UTC). By default, all bucketing and
  293. rounding is also done in UTC. Use the `time_zone` parameter to indicate
  294. that bucketing should use a different time zone.
  295. For example, if the interval is a calendar day and the time zone is
  296. `America/New_York` then `2020-01-03T01:00:01Z` is :
  297. # Converted to `2020-01-02T18:00:01`
  298. # Rounded down to `2020-01-02T00:00:00`
  299. # Then converted back to UTC to produce `2020-01-02T05:00:00:00Z`
  300. # Finally, when the bucket is turned into a string key it is printed in
  301. `America/New_York` so it'll display as `"2020-01-02T00:00:00"`.
  302. It looks like:
  303. [source,java]
  304. ----
  305. bucket_key = localToUtc(Math.floor(utcToLocal(value) / interval) * interval))
  306. ----
  307. You can specify time zones as an ISO 8601 UTC offset (e.g. `+01:00` or
  308. `-08:00`) or as an IANA time zone ID,
  309. such as `America/Los_Angeles`.
  310. Consider the following example:
  311. [source,console,id=datehistogram-aggregation-timezone-example]
  312. ---------------------------------
  313. PUT my-index-000001/_doc/1?refresh
  314. {
  315. "date": "2015-10-01T00:30:00Z"
  316. }
  317. PUT my-index-000001/_doc/2?refresh
  318. {
  319. "date": "2015-10-01T01:30:00Z"
  320. }
  321. GET my-index-000001/_search?size=0
  322. {
  323. "aggs": {
  324. "by_day": {
  325. "date_histogram": {
  326. "field": "date",
  327. "calendar_interval": "day"
  328. }
  329. }
  330. }
  331. }
  332. ---------------------------------
  333. If you don't specify a time zone, UTC is used. This would result in both of these
  334. documents being placed into the same day bucket, which starts at midnight UTC
  335. on 1 October 2015:
  336. [source,console-result]
  337. ---------------------------------
  338. {
  339. ...
  340. "aggregations": {
  341. "by_day": {
  342. "buckets": [
  343. {
  344. "key_as_string": "2015-10-01T00:00:00.000Z",
  345. "key": 1443657600000,
  346. "doc_count": 2
  347. }
  348. ]
  349. }
  350. }
  351. }
  352. ---------------------------------
  353. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  354. If you specify a `time_zone` of `-01:00`, midnight in that time zone is one hour
  355. before midnight UTC:
  356. [source,console]
  357. ---------------------------------
  358. GET my-index-000001/_search?size=0
  359. {
  360. "aggs": {
  361. "by_day": {
  362. "date_histogram": {
  363. "field": "date",
  364. "calendar_interval": "day",
  365. "time_zone": "-01:00"
  366. }
  367. }
  368. }
  369. }
  370. ---------------------------------
  371. // TEST[continued]
  372. Now the first document falls into the bucket for 30 September 2015, while the
  373. second document falls into the bucket for 1 October 2015:
  374. [source,console-result]
  375. ---------------------------------
  376. {
  377. ...
  378. "aggregations": {
  379. "by_day": {
  380. "buckets": [
  381. {
  382. "key_as_string": "2015-09-30T00:00:00.000-01:00", <1>
  383. "key": 1443574800000,
  384. "doc_count": 1
  385. },
  386. {
  387. "key_as_string": "2015-10-01T00:00:00.000-01:00", <1>
  388. "key": 1443661200000,
  389. "doc_count": 1
  390. }
  391. ]
  392. }
  393. }
  394. }
  395. ---------------------------------
  396. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  397. <1> The `key_as_string` value represents midnight on each day
  398. in the specified time zone.
  399. WARNING: Many time zones shift their clocks for daylight savings time. Buckets
  400. close to the moment when those changes happen can have slightly different sizes
  401. than you would expect from the `calendar_interval` or `fixed_interval`.
  402. For example, consider a DST start in the `CET` time zone: on 27 March 2016 at 2am,
  403. clocks were turned forward 1 hour to 3am local time. If you use `day` as the
  404. `calendar_interval`, the bucket covering that day will only hold data for 23
  405. hours instead of the usual 24 hours for other buckets. The same is true for
  406. shorter intervals, like a `fixed_interval` of `12h`, where you'll have only a 11h
  407. bucket on the morning of 27 March when the DST shift happens.
  408. [[search-aggregations-bucket-datehistogram-offset]]
  409. ==== Offset
  410. // tag::offset-explanation[]
  411. Use the `offset` parameter to change the start value of each bucket by the
  412. specified positive (`+`) or negative offset (`-`) duration, such as `1h` for
  413. an hour, or `1d` for a day. See <<time-units>> for more possible time
  414. duration options.
  415. For example, when using an interval of `day`, each bucket runs from midnight
  416. to midnight. Setting the `offset` parameter to `+6h` changes each bucket
  417. to run from 6am to 6am:
  418. // end::offset-explanation[]
  419. [source,console,id=datehistogram-aggregation-offset-example]
  420. -----------------------------
  421. PUT my-index-000001/_doc/1?refresh
  422. {
  423. "date": "2015-10-01T05:30:00Z"
  424. }
  425. PUT my-index-000001/_doc/2?refresh
  426. {
  427. "date": "2015-10-01T06:30:00Z"
  428. }
  429. GET my-index-000001/_search?size=0
  430. {
  431. "aggs": {
  432. "by_day": {
  433. "date_histogram": {
  434. "field": "date",
  435. "calendar_interval": "day",
  436. "offset": "+6h"
  437. }
  438. }
  439. }
  440. }
  441. -----------------------------
  442. // tag::offset-result-intro[]
  443. Instead of a single bucket starting at midnight, the above request groups the
  444. documents into buckets starting at 6am:
  445. // end::offset-result-intro[]
  446. [source,console-result]
  447. -----------------------------
  448. {
  449. ...
  450. "aggregations": {
  451. "by_day": {
  452. "buckets": [
  453. {
  454. "key_as_string": "2015-09-30T06:00:00.000Z",
  455. "key": 1443592800000,
  456. "doc_count": 1
  457. },
  458. {
  459. "key_as_string": "2015-10-01T06:00:00.000Z",
  460. "key": 1443679200000,
  461. "doc_count": 1
  462. }
  463. ]
  464. }
  465. }
  466. }
  467. -----------------------------
  468. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  469. // tag::offset-note[]
  470. NOTE: The start `offset` of each bucket is calculated after `time_zone`
  471. adjustments have been made.
  472. // end::offset-note[]
  473. [[date-histogram-keyed-response]]
  474. ==== Keyed Response
  475. Setting the `keyed` flag to `true` associates a unique string key with each
  476. bucket and returns the ranges as a hash rather than an array:
  477. [source,console,id=datehistogram-aggregation-keyed-example]
  478. --------------------------------------------------
  479. POST /sales/_search?size=0
  480. {
  481. "aggs": {
  482. "sales_over_time": {
  483. "date_histogram": {
  484. "field": "date",
  485. "calendar_interval": "1M",
  486. "format": "yyyy-MM-dd",
  487. "keyed": true
  488. }
  489. }
  490. }
  491. }
  492. --------------------------------------------------
  493. // TEST[setup:sales]
  494. Response:
  495. [source,console-result]
  496. --------------------------------------------------
  497. {
  498. ...
  499. "aggregations": {
  500. "sales_over_time": {
  501. "buckets": {
  502. "2015-01-01": {
  503. "key_as_string": "2015-01-01",
  504. "key": 1420070400000,
  505. "doc_count": 3
  506. },
  507. "2015-02-01": {
  508. "key_as_string": "2015-02-01",
  509. "key": 1422748800000,
  510. "doc_count": 2
  511. },
  512. "2015-03-01": {
  513. "key_as_string": "2015-03-01",
  514. "key": 1425168000000,
  515. "doc_count": 2
  516. }
  517. }
  518. }
  519. }
  520. }
  521. --------------------------------------------------
  522. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  523. [[date-histogram-scripts]]
  524. ==== Scripts
  525. If the data in your documents doesn't exactly match what you'd like to aggregate,
  526. use a <<runtime,runtime field>> . For example, if the revenue
  527. for promoted sales should be recognized a day after the sale date:
  528. [source,console,id=datehistogram-aggregation-runtime-field]
  529. ----
  530. POST /sales/_search?size=0
  531. {
  532. "runtime_mappings": {
  533. "date.promoted_is_tomorrow": {
  534. "type": "date",
  535. "script": """
  536. long date = doc['date'].value.toInstant().toEpochMilli();
  537. if (doc['promoted'].value) {
  538. date += 86400;
  539. }
  540. emit(date);
  541. """
  542. }
  543. },
  544. "aggs": {
  545. "sales_over_time": {
  546. "date_histogram": {
  547. "field": "date.promoted_is_tomorrow",
  548. "calendar_interval": "1M"
  549. }
  550. }
  551. }
  552. }
  553. ----
  554. // TEST[setup:sales]
  555. ////
  556. [source,console-result]
  557. ----
  558. {
  559. ...
  560. "aggregations": {
  561. "sales_over_time": {
  562. "buckets": [
  563. {
  564. "key_as_string": "2015-01-01T00:00:00.000Z",
  565. "key": 1420070400000,
  566. "doc_count": 3
  567. },
  568. {
  569. "key_as_string": "2015-02-01T00:00:00.000Z",
  570. "key": 1422748800000,
  571. "doc_count": 2
  572. },
  573. {
  574. "key_as_string": "2015-03-01T00:00:00.000Z",
  575. "key": 1425168000000,
  576. "doc_count": 2
  577. }
  578. ]
  579. }
  580. }
  581. }
  582. ----
  583. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  584. ////
  585. [[date-histogram-params]]
  586. ==== Parameters
  587. You can control the order of the returned
  588. buckets using the `order`
  589. settings and filter the returned buckets based on a `min_doc_count` setting
  590. (by default all buckets between the first
  591. bucket that matches documents and the last one are returned). This histogram
  592. also supports the `extended_bounds`
  593. setting, which enables extending the bounds of the histogram beyond the data
  594. itself, and `hard_bounds` that limits the histogram to specified bounds.
  595. For more information, see
  596. <<search-aggregations-bucket-histogram-aggregation-extended-bounds,`Extended Bounds`>> and
  597. <<search-aggregations-bucket-histogram-aggregation-hard-bounds,`Hard Bounds`>>.
  598. [[date-histogram-missing-value]]
  599. ===== Missing value
  600. The `missing` parameter defines how to treat documents that are missing a value.
  601. By default, they are ignored, but it is also possible to treat them as if they
  602. have a value.
  603. [source,console,id=datehistogram-aggregation-missing-example]
  604. --------------------------------------------------
  605. POST /sales/_search?size=0
  606. {
  607. "aggs": {
  608. "sale_date": {
  609. "date_histogram": {
  610. "field": "date",
  611. "calendar_interval": "year",
  612. "missing": "2000/01/01" <1>
  613. }
  614. }
  615. }
  616. }
  617. --------------------------------------------------
  618. // TEST[setup:sales]
  619. <1> Documents without a value in the `publish_date` field will fall into the
  620. same bucket as documents that have the value `2000-01-01`.
  621. [[date-histogram-order]]
  622. ===== Order
  623. By default the returned buckets are sorted by their `key` ascending, but you can
  624. control the order using
  625. the `order` setting. This setting supports the same `order` functionality as
  626. <<search-aggregations-bucket-terms-aggregation-order,`Terms Aggregation`>>.
  627. [[date-histogram-aggregate-scripts]]
  628. ===== Using a script to aggregate by day of the week
  629. When you need to aggregate the results by day of the week, run a `terms`
  630. aggregation on a <<runtime,runtime field>> that returns the day of the week:
  631. [source,console,id=datehistogram-aggregation-day-of-week-runtime-field]
  632. ----
  633. POST /sales/_search?size=0
  634. {
  635. "runtime_mappings": {
  636. "date.day_of_week": {
  637. "type": "keyword",
  638. "script": "emit(doc['date'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT))"
  639. }
  640. },
  641. "aggs": {
  642. "day_of_week": {
  643. "terms": { "field": "date.day_of_week" }
  644. }
  645. }
  646. }
  647. ----
  648. // TEST[setup:sales]
  649. Response:
  650. [source,console-result]
  651. ----
  652. {
  653. ...
  654. "aggregations": {
  655. "day_of_week": {
  656. "doc_count_error_upper_bound": 0,
  657. "sum_other_doc_count": 0,
  658. "buckets": [
  659. {
  660. "key": "Sunday",
  661. "doc_count": 4
  662. },
  663. {
  664. "key": "Thursday",
  665. "doc_count": 3
  666. }
  667. ]
  668. }
  669. }
  670. }
  671. ----
  672. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  673. The response will contain all the buckets having the relative day of
  674. the week as key : 1 for Monday, 2 for Tuesday... 7 for Sunday.