date-histogram-facet.asciidoc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. [[search-facets-date-histogram-facet]]
  2. === Date Histogram Facet
  3. include::deprecated.asciidoc[]
  4. A specific histogram facet that can work with `date` field types
  5. enhancing it over the regular
  6. <<search-facets-histogram-facet,histogram
  7. facet>>. Here is a quick example:
  8. [source,js]
  9. --------------------------------------------------
  10. {
  11. "query" : {
  12. "match_all" : {}
  13. },
  14. "facets" : {
  15. "histo1" : {
  16. "date_histogram" : {
  17. "field" : "field_name",
  18. "interval" : "day"
  19. }
  20. }
  21. }
  22. }
  23. --------------------------------------------------
  24. ==== Interval
  25. The `interval` allows to set the interval at which buckets will be
  26. created for each hit. It allows for the constant values of `year`,
  27. `quarter`, `month`, `week`, `day`, `hour`, `minute` ,`second`.
  28. It also support time setting like `1.5h` (up to `w` for weeks).
  29. ==== Time Zone
  30. By default, times are stored as UTC milliseconds since the epoch. Thus,
  31. all computation and "bucketing" / "rounding" is done on UTC. It is
  32. possible to provide a time zone (both pre rounding, and post rounding)
  33. value, which will cause all computations to take the relevant zone into
  34. account. The time returned for each bucket/entry is milliseconds since
  35. the epoch of the provided time zone.
  36. The parameters are `pre_zone` (pre rounding based on interval) and
  37. `post_zone` (post rounding based on interval). The `time_zone` parameter
  38. simply sets the `pre_zone` parameter. By default, those are set to
  39. `UTC`.
  40. The zone value accepts either a numeric value for the hours offset, for
  41. example: `"time_zone" : -2`. It also accepts a format of hours and
  42. minutes, like `"time_zone" : "-02:30"`. Another option is to provide a
  43. time zone accepted as one of the values listed
  44. http://joda-time.sourceforge.net/timezones.html[here].
  45. Lets take an example. For `2012-04-01T04:15:30Z`, with a `pre_zone` of
  46. `-08:00`. For `day` interval, the actual time by applying the time zone
  47. and rounding falls under `2012-03-31`, so the returned value will be (in
  48. millis) of `2012-03-31T00:00:00Z` (UTC). For `hour` interval, applying
  49. the time zone results in `2012-03-31T20:15:30`, rounding it results in
  50. `2012-03-31T20:00:00`, but, we want to return it in UTC (`post_zone` is
  51. not set), so we convert it back to UTC: `2012-04-01T04:00:00Z`. Note, we
  52. are consistent in the results, returning the rounded value in UTC.
  53. `post_zone` simply takes the result, and adds the relevant offset.
  54. Sometimes, we want to apply the same conversion to UTC we did above for
  55. `hour` also for `day` (and up) intervals. We can set
  56. `pre_zone_adjust_large_interval` to `true`, which will apply the same
  57. conversion done for `hour` interval in the example, to `day` and above
  58. intervals (it can be set regardless of the interval, but only kick in
  59. when using `day` and higher intervals).
  60. ==== Factor
  61. The date histogram works on numeric values (since time is stored in
  62. milliseconds since the epoch in UTC). But, sometimes, systems will store
  63. a different resolution (like seconds since UTC) in a numeric field. The
  64. `factor` parameter can be used to change the value in the field to
  65. milliseconds to actual do the relevant rounding, and then be applied
  66. again to get to the original unit. For example, when storing in a
  67. numeric field seconds resolution, the `factor` can be set to `1000`.
  68. ==== Pre / Post Offset
  69. Specific offsets can be provided for pre rounding and post rounding. The
  70. `pre_offset` for pre rounding, and `post_offset` for post rounding. The
  71. format is the date time format (`1h`, `1d`, ...).
  72. ==== Value Field
  73. The date_histogram facet allows to use a different key (of type date)
  74. which controls the bucketing, with a different value field which will
  75. then return the total and mean for that field values of the hits within
  76. the relevant bucket. For example:
  77. [source,js]
  78. --------------------------------------------------
  79. {
  80. "query" : {
  81. "match_all" : {}
  82. },
  83. "facets" : {
  84. "histo1" : {
  85. "date_histogram" : {
  86. "key_field" : "timestamp",
  87. "value_field" : "price",
  88. "interval" : "day"
  89. }
  90. }
  91. }
  92. }
  93. --------------------------------------------------
  94. ==== Script Value Field
  95. A script can be used to compute the value that will then be used to
  96. compute the total and mean for a bucket. For example:
  97. [source,js]
  98. --------------------------------------------------
  99. {
  100. "query" : {
  101. "match_all" : {}
  102. },
  103. "facets" : {
  104. "histo1" : {
  105. "date_histogram" : {
  106. "key_field" : "timestamp",
  107. "value_script" : "doc['price'].value * 2",
  108. "interval" : "day"
  109. }
  110. }
  111. }
  112. }
  113. --------------------------------------------------