elasticsearch/docs/reference/esql/functions/bucket.asciidoc
Bogdan Pintea a2c2e8fe47
ESQL: extend BUCKET with spans. Turn it into a grouping function (#107272)
This extends `BUCKET` function to accept a two-parameters-only
invocation: the first parameter remains as is, while the second is a
span. It can be a numeric (floating point) span, if the first argument
is numeric, or a date period or time duration, if the first argument is
a date.

Also, the function can now be invoked with the alias BIN.

Additionally, the function has been turned into a grouping-only function
and thus can only be used within a `STATS` command.
2024-04-16 12:57:18 +02:00

118 lines
3.3 KiB
Text

[discrete]
[[esql-bucket]]
=== `BUCKET`
*Syntax*
[source,esql]
----
BUCKET(expression, buckets, from, to)
----
*Parameters*
`field`::
Numeric or date expression from which to derive buckets.
`buckets`::
Target number of buckets.
`from`::
Start of the range. Can be a number or a date expressed as a string.
`to`::
End of the range. Can be a number or a date expressed as a string.
*Description*
Creates human-friendly buckets and returns a value for each row that corresponds
to the resulting bucket the row falls into.
Using a target number of buckets, a start of a range, and an end of a range,
`BUCKET` picks an appropriate bucket size to generate the target number of
buckets or fewer. For example, asking for at most 20 buckets over a year results
in monthly buckets:
[source.merge.styled,esql]
----
include::{esql-specs}/bucket.csv-spec[tag=docsBucketMonth]
----
[%header.monospaced.styled,format=dsv,separator=|]
|===
include::{esql-specs}/bucket.csv-spec[tag=docsBucketMonth-result]
|===
The goal isn't to provide *exactly* the target number of buckets, it's to pick a
range that people are comfortable with that provides at most the target number
of buckets.
Combine `BUCKET` with
<<esql-stats-by>> to create a histogram:
[source.merge.styled,esql]
----
include::{esql-specs}/bucket.csv-spec[tag=docsBucketMonthlyHistogram]
----
[%header.monospaced.styled,format=dsv,separator=|]
|===
include::{esql-specs}/bucket.csv-spec[tag=docsBucketMonthlyHistogram-result]
|===
NOTE: `BUCKET` does not create buckets that don't match any documents.
That's why this example is missing `1985-03-01` and other dates.
Asking for more buckets can result in a smaller range. For example, asking for
at most 100 buckets in a year results in weekly buckets:
[source.merge.styled,esql]
----
include::{esql-specs}/bucket.csv-spec[tag=docsBucketWeeklyHistogram]
----
[%header.monospaced.styled,format=dsv,separator=|]
|===
include::{esql-specs}/bucket.csv-spec[tag=docsBucketWeeklyHistogram-result]
|===
NOTE: `BUCKET` does not filter any rows. It only uses the provided range to
pick a good bucket size. For rows with a value outside of the range, it returns
a bucket value that corresponds to a bucket outside the range. Combine
`BUCKET` with <<esql-where>> to filter rows.
`BUCKET` can also operate on numeric fields. For example, to create a
salary histogram:
[source.merge.styled,esql]
----
include::{esql-specs}/bucket.csv-spec[tag=docsBucketNumeric]
----
[%header.monospaced.styled,format=dsv,separator=|]
|===
include::{esql-specs}/bucket.csv-spec[tag=docsBucketNumeric-result]
|===
Unlike the earlier example that intentionally filters on a date range, you
rarely want to filter on a numeric range. You have to find the `min` and `max`
separately. {esql} doesn't yet have an easy way to do that automatically.
*Examples*
Create hourly buckets for the last 24 hours, and calculate the number of events
per hour:
[source.styled,esql]
----
include::{esql-specs}/bucket.csv-spec[tag=docsBucketLast24hr]
----
Create monthly buckets for the year 1985, and calculate the average salary by
hiring month:
[source.merge.styled,esql]
----
include::{esql-specs}/bucket.csv-spec[tag=bucket_in_agg]
----
[%header.monospaced.styled,format=dsv,separator=|]
|===
include::{esql-specs}/bucket.csv-spec[tag=bucket_in_agg-result]
|===