auto_bucket.asciidoc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. [discrete]
  2. [[esql-auto_bucket]]
  3. === `AUTO_BUCKET`
  4. *Syntax*
  5. [source,esql]
  6. ----
  7. AUTO_BUCKET(expression, buckets, from, to)
  8. ----
  9. *Parameters*
  10. `field`::
  11. Numeric or date expression from which to derive buckets.
  12. `buckets`::
  13. Target number of buckets.
  14. `from`::
  15. Start of the range. Can be a number or a date expressed as a string.
  16. `to`::
  17. End of the range. Can be a number or a date expressed as a string.
  18. *Description*
  19. Creates human-friendly buckets and returns a value for each row that corresponds
  20. to the resulting bucket the row falls into.
  21. Using a target number of buckets, a start of a range, and an end of a range,
  22. `AUTO_BUCKET` picks an appropriate bucket size to generate the target number of
  23. buckets or fewer. For example, asking for at most 20 buckets over a year results
  24. in monthly buckets:
  25. [source.merge.styled,esql]
  26. ----
  27. include::{esql-specs}/date.csv-spec[tag=docsAutoBucketMonth]
  28. ----
  29. [%header.monospaced.styled,format=dsv,separator=|]
  30. |===
  31. include::{esql-specs}/date.csv-spec[tag=docsAutoBucketMonth-result]
  32. |===
  33. The goal isn't to provide *exactly* the target number of buckets, it's to pick a
  34. range that people are comfortable with that provides at most the target number
  35. of buckets.
  36. Combine `AUTO_BUCKET` with
  37. <<esql-stats-by>> to create a histogram:
  38. [source.merge.styled,esql]
  39. ----
  40. include::{esql-specs}/date.csv-spec[tag=docsAutoBucketMonthlyHistogram]
  41. ----
  42. [%header.monospaced.styled,format=dsv,separator=|]
  43. |===
  44. include::{esql-specs}/date.csv-spec[tag=docsAutoBucketMonthlyHistogram-result]
  45. |===
  46. NOTE: `AUTO_BUCKET` does not create buckets that don't match any documents.
  47. That's why this example is missing `1985-03-01` and other dates.
  48. Asking for more buckets can result in a smaller range. For example, asking for
  49. at most 100 buckets in a year results in weekly buckets:
  50. [source.merge.styled,esql]
  51. ----
  52. include::{esql-specs}/date.csv-spec[tag=docsAutoBucketWeeklyHistogram]
  53. ----
  54. [%header.monospaced.styled,format=dsv,separator=|]
  55. |===
  56. include::{esql-specs}/date.csv-spec[tag=docsAutoBucketWeeklyHistogram-result]
  57. |===
  58. NOTE: `AUTO_BUCKET` does not filter any rows. It only uses the provided range to
  59. pick a good bucket size. For rows with a value outside of the range, it returns
  60. a bucket value that corresponds to a bucket outside the range. Combine
  61. `AUTO_BUCKET` with <<esql-where>> to filter rows.
  62. `AUTO_BUCKET` can also operate on numeric fields. For example, to create a
  63. salary histogram:
  64. [source.merge.styled,esql]
  65. ----
  66. include::{esql-specs}/ints.csv-spec[tag=docsAutoBucketNumeric]
  67. ----
  68. [%header.monospaced.styled,format=dsv,separator=|]
  69. |===
  70. include::{esql-specs}/ints.csv-spec[tag=docsAutoBucketNumeric-result]
  71. |===
  72. Unlike the earlier example that intentionally filters on a date range, you
  73. rarely want to filter on a numeric range. You have to find the `min` and `max`
  74. separately. {esql} doesn't yet have an easy way to do that automatically.
  75. *Examples*
  76. Create hourly buckets for the last 24 hours, and calculate the number of events
  77. per hour:
  78. [source.styled,esql]
  79. ----
  80. include::{esql-specs}/date.csv-spec[tag=docsAutoBucketLast24hr]
  81. ----
  82. Create monthly buckets for the year 1985, and calculate the average salary by
  83. hiring month:
  84. [source.merge.styled,esql]
  85. ----
  86. include::{esql-specs}/date.csv-spec[tag=auto_bucket_in_agg]
  87. ----
  88. [%header.monospaced.styled,format=dsv,separator=|]
  89. |===
  90. include::{esql-specs}/date.csv-spec[tag=auto_bucket_in_agg-result]
  91. |===