range-field-note.asciidoc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. [[search-aggregations-bucket-range-field-note]]
  2. === Subtleties of bucketing range fields
  3. ==== Documents are counted for each bucket they land in
  4. Since a range represents multiple values, running a bucket aggregation over a
  5. range field can result in the same document landing in multiple buckets. This
  6. can lead to surprising behavior, such as the sum of bucket counts being higher
  7. than the number of matched documents. For example, consider the following
  8. index:
  9. [source, console]
  10. --------------------------------------------------
  11. PUT range_index
  12. {
  13. "settings": {
  14. "number_of_shards": 2
  15. },
  16. "mappings": {
  17. "properties": {
  18. "expected_attendees": {
  19. "type": "integer_range"
  20. },
  21. "time_frame": {
  22. "type": "date_range",
  23. "format": "yyyy-MM-dd||epoch_millis"
  24. }
  25. }
  26. }
  27. }
  28. PUT range_index/_doc/1?refresh
  29. {
  30. "expected_attendees" : {
  31. "gte" : 10,
  32. "lte" : 20
  33. },
  34. "time_frame" : {
  35. "gte" : "2019-10-28",
  36. "lte" : "2019-11-04"
  37. }
  38. }
  39. --------------------------------------------------
  40. // TESTSETUP
  41. The range is wider than the interval in the following aggregation, and thus the
  42. document will land in multiple buckets.
  43. [source, console,id=range-field-aggregation-example]
  44. --------------------------------------------------
  45. POST /range_index/_search?size=0
  46. {
  47. "aggs": {
  48. "range_histo": {
  49. "histogram": {
  50. "field": "expected_attendees",
  51. "interval": 5
  52. }
  53. }
  54. }
  55. }
  56. --------------------------------------------------
  57. Since the interval is `5` (and the offset is `0` by default), we expect buckets `10`,
  58. `15`, and `20`. Our range document will fall in all three of these buckets.
  59. [source, console-result]
  60. --------------------------------------------------
  61. {
  62. ...
  63. "aggregations" : {
  64. "range_histo" : {
  65. "buckets" : [
  66. {
  67. "key" : 10.0,
  68. "doc_count" : 1
  69. },
  70. {
  71. "key" : 15.0,
  72. "doc_count" : 1
  73. },
  74. {
  75. "key" : 20.0,
  76. "doc_count" : 1
  77. }
  78. ]
  79. }
  80. }
  81. }
  82. --------------------------------------------------
  83. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  84. A document cannot exist partially in a bucket; For example, the above document
  85. cannot count as one-third in each of the above three buckets. In this example,
  86. since the document's range landed in multiple buckets, the full value of that
  87. document would also be counted in any sub-aggregations for each bucket as well.
  88. ==== Query bounds are not aggregation filters
  89. Another unexpected behavior can arise when a query is used to filter on the
  90. field being aggregated. In this case, a document could match the query but
  91. still have one or both of the endpoints of the range outside the query.
  92. Consider the following aggregation on the above document:
  93. [source, console,id=range-field-aggregation-query-bounds-example]
  94. --------------------------------------------------
  95. POST /range_index/_search?size=0
  96. {
  97. "query": {
  98. "range": {
  99. "time_frame": {
  100. "gte": "2019-11-01",
  101. "format": "yyyy-MM-dd"
  102. }
  103. }
  104. },
  105. "aggs": {
  106. "november_data": {
  107. "date_histogram": {
  108. "field": "time_frame",
  109. "calendar_interval": "day",
  110. "format": "yyyy-MM-dd"
  111. }
  112. }
  113. }
  114. }
  115. --------------------------------------------------
  116. Even though the query only considers days in November, the aggregation
  117. generates 8 buckets (4 in October, 4 in November) because the aggregation is
  118. calculated over the ranges of all matching documents.
  119. [source, console-result]
  120. --------------------------------------------------
  121. {
  122. ...
  123. "aggregations" : {
  124. "november_data" : {
  125. "buckets" : [
  126. {
  127. "key_as_string" : "2019-10-28",
  128. "key" : 1572220800000,
  129. "doc_count" : 1
  130. },
  131. {
  132. "key_as_string" : "2019-10-29",
  133. "key" : 1572307200000,
  134. "doc_count" : 1
  135. },
  136. {
  137. "key_as_string" : "2019-10-30",
  138. "key" : 1572393600000,
  139. "doc_count" : 1
  140. },
  141. {
  142. "key_as_string" : "2019-10-31",
  143. "key" : 1572480000000,
  144. "doc_count" : 1
  145. },
  146. {
  147. "key_as_string" : "2019-11-01",
  148. "key" : 1572566400000,
  149. "doc_count" : 1
  150. },
  151. {
  152. "key_as_string" : "2019-11-02",
  153. "key" : 1572652800000,
  154. "doc_count" : 1
  155. },
  156. {
  157. "key_as_string" : "2019-11-03",
  158. "key" : 1572739200000,
  159. "doc_count" : 1
  160. },
  161. {
  162. "key_as_string" : "2019-11-04",
  163. "key" : 1572825600000,
  164. "doc_count" : 1
  165. }
  166. ]
  167. }
  168. }
  169. }
  170. --------------------------------------------------
  171. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  172. Depending on the use case, a `CONTAINS` query could limit the documents to only
  173. those that fall entirely in the queried range. In this example, the one
  174. document would not be included and the aggregation would be empty. Filtering
  175. the buckets after the aggregation is also an option, for use cases where the
  176. document should be counted but the out of bounds data can be safely ignored.