Skip to content

Commit

Permalink
ESQL: Docs: add example of date bucketing with offset (elastic#116680)
Browse files Browse the repository at this point in the history
Add an example of how to create date histograms with an offset.

Fixes elastic#114167
  • Loading branch information
bpintea committed Dec 18, 2024
1 parent a355f19 commit 63ab3be
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 4 deletions.
14 changes: 14 additions & 0 deletions docs/reference/esql/functions/examples/bucket.asciidoc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion docs/reference/esql/functions/kibana/definition/bucket.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
import static org.elasticsearch.xpack.esql.core.util.SpatialCoordinateTypes.GEO;

public final class CsvTestUtils {
private static final int MAX_WIDTH = 20;
private static final int MAX_WIDTH = 80;
private static final CsvPreference CSV_SPEC_PREFERENCES = new CsvPreference.Builder('"', '|', "\r\n").build();
private static final String NULL_VALUE = "null";
private static final char ESCAPE_CHAR = '\\';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,24 @@ AVG(salary):double | bucket:date
// end::bucket_in_agg-result[]
;

bucketWithOffset#[skip:-8.13.99, reason:BUCKET renamed in 8.14]
// tag::bucketWithOffset[]
FROM employees
| STATS dates = MV_SORT(VALUES(birth_date)) BY b = BUCKET(birth_date + 1 HOUR, 1 YEAR) - 1 HOUR
| EVAL d_count = MV_COUNT(dates)
| SORT d_count, b
| LIMIT 3
// end::bucketWithOffset[]
;

// tag::bucketWithOffset-result[]
dates:date |b:date |d_count:integer
1965-01-03T00:00:00.000Z |1964-12-31T23:00:00.000Z|1
[1955-01-21T00:00:00.000Z, 1955-08-20T00:00:00.000Z, 1955-08-28T00:00:00.000Z, 1955-10-04T00:00:00.000Z]|1954-12-31T23:00:00.000Z|4
[1957-04-04T00:00:00.000Z, 1957-05-23T00:00:00.000Z, 1957-05-25T00:00:00.000Z, 1957-12-03T00:00:00.000Z]|1956-12-31T23:00:00.000Z|4
// end::bucketWithOffset-result[]
;

docsBucketMonth#[skip:-8.13.99, reason:BUCKET renamed in 8.14]
//tag::docsBucketMonth[]
FROM employees
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,17 @@ another in which the bucket size is provided directly (two parameters).
grouping part, or that it is invoked with the exact same expression:""",
file = "bucket",
tag = "reuseGroupingFunctionWithExpression"
),
@Example(
description = """
Sometimes you need to change the start value of each bucket by a given duration (similar to date histogram
aggregation's <<search-aggregations-bucket-histogram-aggregation,`offset`>> parameter). To do so, you will need to
take into account how the language handles expressions within the `STATS` command: if these contain functions or
arithmetic operators, a virtual `EVAL` is inserted before and/or after the `STATS` command. Consequently, a double
compensation is needed to adjust the bucketed date value before the aggregation and then again after. For instance,
inserting a negative offset of `1 hour` to buckets of `1 year` looks like this:""",
file = "bucket",
tag = "bucketWithOffset"
) }
)
public Bucket(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,14 @@ private void doTest() throws Exception {
}

protected void assertResults(ExpectedResults expected, ActualResults actual, boolean ignoreOrder, Logger logger) {
CsvAssert.assertResults(expected, actual, ignoreOrder, logger);
/*
* Comment the assertion above and enable the next two lines to see the results returned by ES without any assertions being done.
* Enable the next two lines to see the results returned by ES.
* This is useful when creating a new test or trying to figure out what are the actual results.
*/
// CsvTestUtils.logMetaData(actual.columnNames(), actual.columnTypes(), LOGGER);
// CsvTestUtils.logData(actual.values(), LOGGER);

CsvAssert.assertResults(expected, actual, ignoreOrder, logger);
}

private static IndexResolution loadIndexResolution(String mappingName, String indexName, Map<String, String> typeMapping) {
Expand Down

0 comments on commit 63ab3be

Please sign in to comment.