bucket.asciidoc 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // This is generated by ESQL's AbstractFunctionTestCase. Do no edit it. See ../README.md for how to regenerate it.
  2. *Examples*
  3. Using a target number of buckets, a start of a range, and an end of a range,
  4. `BUCKET` picks an appropriate bucket size to generate the target number of buckets or fewer.
  5. For example, asking for at most 20 buckets over a year results in monthly buckets:
  6. [source.merge.styled,esql]
  7. ----
  8. include::{esql-specs}/bucket.csv-spec[tag=docsBucketMonth]
  9. ----
  10. [%header.monospaced.styled,format=dsv,separator=|]
  11. |===
  12. include::{esql-specs}/bucket.csv-spec[tag=docsBucketMonth-result]
  13. |===
  14. The goal isn't to provide *exactly* the target number of buckets,
  15. it's to pick a range that people are comfortable with that provides at most the target number of buckets.
  16. Combine `BUCKET` with <<esql-stats-by>> to create a histogram:
  17. [source.merge.styled,esql]
  18. ----
  19. include::{esql-specs}/bucket.csv-spec[tag=docsBucketMonthlyHistogram]
  20. ----
  21. [%header.monospaced.styled,format=dsv,separator=|]
  22. |===
  23. include::{esql-specs}/bucket.csv-spec[tag=docsBucketMonthlyHistogram-result]
  24. |===
  25. NOTE: `BUCKET` does not create buckets that don't match any documents.
  26. + "That's why this example is missing `1985-03-01` and other dates.
  27. Asking for more buckets can result in a smaller range.
  28. For example, asking for at most 100 buckets in a year results in weekly buckets:
  29. [source.merge.styled,esql]
  30. ----
  31. include::{esql-specs}/bucket.csv-spec[tag=docsBucketWeeklyHistogram]
  32. ----
  33. [%header.monospaced.styled,format=dsv,separator=|]
  34. |===
  35. include::{esql-specs}/bucket.csv-spec[tag=docsBucketWeeklyHistogram-result]
  36. |===
  37. NOTE: `BUCKET` does not filter any rows. It only uses the provided range to pick a good bucket size.
  38. For rows with a value outside of the range, it returns a bucket value that corresponds to a bucket outside the range.
  39. Combine`BUCKET` with <<esql-where>> to filter rows.
  40. `BUCKET` can also operate on numeric fields. For example, to create a salary histogram:
  41. [source.merge.styled,esql]
  42. ----
  43. include::{esql-specs}/bucket.csv-spec[tag=docsBucketNumeric]
  44. ----
  45. [%header.monospaced.styled,format=dsv,separator=|]
  46. |===
  47. include::{esql-specs}/bucket.csv-spec[tag=docsBucketNumeric-result]
  48. |===
  49. Unlike the earlier example that intentionally filters on a date range, you rarely want to filter on a numeric range.
  50. You have to find the `min` and `max` separately. {esql} doesn't yet have an easy way to do that automatically.
  51. Create hourly buckets for the last 24 hours, and calculate the number of events per hour:
  52. [source.merge.styled,esql]
  53. ----
  54. include::{esql-specs}/bucket.csv-spec[tag=docsBucketLast24hr]
  55. ----
  56. [%header.monospaced.styled,format=dsv,separator=|]
  57. |===
  58. include::{esql-specs}/bucket.csv-spec[tag=docsBucketLast24hr-result]
  59. |===
  60. Create monthly buckets for the year 1985, and calculate the average salary by hiring month
  61. [source.merge.styled,esql]
  62. ----
  63. include::{esql-specs}/bucket.csv-spec[tag=bucket_in_agg]
  64. ----
  65. [%header.monospaced.styled,format=dsv,separator=|]
  66. |===
  67. include::{esql-specs}/bucket.csv-spec[tag=bucket_in_agg-result]
  68. |===