auto_bucket.asciidoc 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. [[esql-auto_bucket]]
  2. === `AUTO_BUCKET`
  3. Creates human-friendly buckets and returns a `datetime` value for each row that
  4. corresponds to the resulting bucket the row falls into. Combine `AUTO_BUCKET`
  5. with <<esql-stats-by>> to create a date histogram.
  6. You provide a target number of buckets, a start date, and an end date, and it
  7. picks an appropriate bucket size to generate the target number of buckets or
  8. fewer. For example, this asks for at most 20 buckets over a whole year, which
  9. picks monthly buckets:
  10. [source,esql]
  11. ----
  12. include::{esql-specs}/date.csv-spec[tag=auto_bucket_month]
  13. ----
  14. Returns:
  15. [%header,format=dsv,separator=|]
  16. |===
  17. include::{esql-specs}/date.csv-spec[tag=auto_bucket_month-result]
  18. |===
  19. The goal isn't to provide *exactly* the target number of buckets, it's to pick a
  20. range that people are comfortable with that provides at most the target number of
  21. buckets.
  22. If you ask for more buckets then `AUTO_BUCKET` can pick a smaller range. For example,
  23. asking for at most 100 buckets in a year will get you week long buckets:
  24. [source,esql]
  25. ----
  26. include::{esql-specs}/date.csv-spec[tag=auto_bucket_week]
  27. ----
  28. Returns:
  29. [%header,format=dsv,separator=|]
  30. |===
  31. include::{esql-specs}/date.csv-spec[tag=auto_bucket_week-result]
  32. |===
  33. `AUTO_BUCKET` does not filter any rows. It only uses the provided time range to
  34. pick a good bucket size. For rows with a date outside of the range, it returns a
  35. `datetime` that corresponds to a bucket outside the range. Combine `AUTO_BUCKET`
  36. with <<esql-where>> to filter rows.
  37. A more complete example might look like:
  38. [source,esql]
  39. ----
  40. include::{esql-specs}/date.csv-spec[tag=auto_bucket_in_agg]
  41. ----
  42. Which returns:
  43. [%header,format=dsv,separator=|]
  44. |===
  45. include::{esql-specs}/date.csv-spec[tag=auto_bucket_in_agg-result]
  46. |===
  47. NOTE: `AUTO_BUCKET` does not create buckets that don't match any documents. That's
  48. why the example above is missing `1985-02-01` and other dates.