bucket.asciidoc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // This is generated by ESQL's AbstractFunctionTestCase. Do no edit it. See ../README.md for how to regenerate it.
  2. *Examples*
  3. `BUCKET` can work in two modes: one in which the size of the bucket is computed
  4. based on a buckets count recommendation (four parameters) and a range, and
  5. another in which the bucket size is provided directly (two parameters).
  6. Using a target number of buckets, a start of a range, and an end of a range,
  7. `BUCKET` picks an appropriate bucket size to generate the target number of buckets or fewer.
  8. For example, asking for at most 20 buckets over a year results in monthly buckets:
  9. [source.merge.styled,esql]
  10. ----
  11. include::{esql-specs}/bucket.csv-spec[tag=docsBucketMonth]
  12. ----
  13. [%header.monospaced.styled,format=dsv,separator=|]
  14. |===
  15. include::{esql-specs}/bucket.csv-spec[tag=docsBucketMonth-result]
  16. |===
  17. The goal isn't to provide *exactly* the target number of buckets,
  18. it's to pick a range that people are comfortable with that provides at most the target number of buckets.
  19. Combine `BUCKET` with an <<esql-agg-functions,aggregation>> to create a histogram:
  20. [source.merge.styled,esql]
  21. ----
  22. include::{esql-specs}/bucket.csv-spec[tag=docsBucketMonthlyHistogram]
  23. ----
  24. [%header.monospaced.styled,format=dsv,separator=|]
  25. |===
  26. include::{esql-specs}/bucket.csv-spec[tag=docsBucketMonthlyHistogram-result]
  27. |===
  28. NOTE: `BUCKET` does not create buckets that don't match any documents.
  29. That's why this example is missing `1985-03-01` and other dates.
  30. Asking for more buckets can result in a smaller range.
  31. For example, asking for at most 100 buckets in a year results in weekly buckets:
  32. [source.merge.styled,esql]
  33. ----
  34. include::{esql-specs}/bucket.csv-spec[tag=docsBucketWeeklyHistogram]
  35. ----
  36. [%header.monospaced.styled,format=dsv,separator=|]
  37. |===
  38. include::{esql-specs}/bucket.csv-spec[tag=docsBucketWeeklyHistogram-result]
  39. |===
  40. NOTE: `BUCKET` does not filter any rows. It only uses the provided range to pick a good bucket size.
  41. For rows with a value outside of the range, it returns a bucket value that corresponds to a bucket outside the range.
  42. Combine`BUCKET` with <<esql-where>> to filter rows.
  43. If the desired bucket size is known in advance, simply provide it as the second
  44. argument, leaving the range out:
  45. [source.merge.styled,esql]
  46. ----
  47. include::{esql-specs}/bucket.csv-spec[tag=docsBucketWeeklyHistogramWithSpan]
  48. ----
  49. [%header.monospaced.styled,format=dsv,separator=|]
  50. |===
  51. include::{esql-specs}/bucket.csv-spec[tag=docsBucketWeeklyHistogramWithSpan-result]
  52. |===
  53. NOTE: When providing the bucket size as the second parameter, it must be a time
  54. duration or date period.
  55. `BUCKET` can also operate on numeric fields. For example, to create a salary histogram:
  56. [source.merge.styled,esql]
  57. ----
  58. include::{esql-specs}/bucket.csv-spec[tag=docsBucketNumeric]
  59. ----
  60. [%header.monospaced.styled,format=dsv,separator=|]
  61. |===
  62. include::{esql-specs}/bucket.csv-spec[tag=docsBucketNumeric-result]
  63. |===
  64. Unlike the earlier example that intentionally filters on a date range, you rarely want to filter on a numeric range.
  65. You have to find the `min` and `max` separately. {esql} doesn't yet have an easy way to do that automatically.
  66. The range can be omitted if the desired bucket size is known in advance. Simply
  67. provide it as the second argument:
  68. [source.merge.styled,esql]
  69. ----
  70. include::{esql-specs}/bucket.csv-spec[tag=docsBucketNumericWithSpan]
  71. ----
  72. [%header.monospaced.styled,format=dsv,separator=|]
  73. |===
  74. include::{esql-specs}/bucket.csv-spec[tag=docsBucketNumericWithSpan-result]
  75. |===
  76. NOTE: When providing the bucket size as the second parameter, it must be
  77. of a floating point type.
  78. Create hourly buckets for the last 24 hours, and calculate the number of events per hour:
  79. [source.merge.styled,esql]
  80. ----
  81. include::{esql-specs}/bucket.csv-spec[tag=docsBucketLast24hr]
  82. ----
  83. [%header.monospaced.styled,format=dsv,separator=|]
  84. |===
  85. include::{esql-specs}/bucket.csv-spec[tag=docsBucketLast24hr-result]
  86. |===
  87. Create monthly buckets for the year 1985, and calculate the average salary by hiring month
  88. [source.merge.styled,esql]
  89. ----
  90. include::{esql-specs}/bucket.csv-spec[tag=bucket_in_agg]
  91. ----
  92. [%header.monospaced.styled,format=dsv,separator=|]
  93. |===
  94. include::{esql-specs}/bucket.csv-spec[tag=bucket_in_agg-result]
  95. |===
  96. `BUCKET` may be used in both the aggregating and grouping part of the
  97. <<esql-stats-by, STATS ... BY ...>> command provided that in the aggregating
  98. part the function is referenced by an alias defined in the
  99. grouping part, or that it is invoked with the exact same expression:
  100. [source.merge.styled,esql]
  101. ----
  102. include::{esql-specs}/bucket.csv-spec[tag=reuseGroupingFunctionWithExpression]
  103. ----
  104. [%header.monospaced.styled,format=dsv,separator=|]
  105. |===
  106. include::{esql-specs}/bucket.csv-spec[tag=reuseGroupingFunctionWithExpression-result]
  107. |===