diff --git a/src/aggregations/bucket-aggregations/date-histogram-aggregation.js b/src/aggregations/bucket-aggregations/date-histogram-aggregation.js index f15f6b6..5469fc6 100644 --- a/src/aggregations/bucket-aggregations/date-histogram-aggregation.js +++ b/src/aggregations/bucket-aggregations/date-histogram-aggregation.js @@ -51,6 +51,50 @@ class DateHistogramAggregation extends HistogramAggregationBase { this._aggsDef.time_zone = tz; return this; } + + /** + * Calendar-aware intervals are configured with the calendarInterval parameter. + * The combined interval field for date histograms is deprecated from ES 7.2. + * + * @example + * const agg = esb.dateHistogramAggregation('by_month', 'date').calendarInterval( + * 'month' + * ); + * + * @param {string} interval Interval to generate histogram over. + * You can specify calendar intervals using the unit name, such as month, or as + * a single unit quantity, such as 1M. For example, day and 1d are equivalent. + * Multiple quantities, such as 2d, are not supported. + * @returns {DateHistogramAggregation} returns `this` so that calls can be chained + */ + calendarInterval(interval) { + this._aggsDef.calendar_interval = interval; + return this; + } + + /** + * Fixed intervals are configured with the fixedInterval parameter. + * The combined interval field for date histograms is deprecated from ES 7.2. + * + * @param {string} interval Interval to generate histogram over. + * Intervals are a fixed number of SI units and never deviate, regardless + * of where they fall on the calendar. However, it means fixed intervals + * cannot express other units such as months, since the duration of a + * month is not a fixed quantity. + * + * @example + * const agg = esb.dateHistogramAggregation('by_minute', 'date').calendarInterval( + * '60s' + * ); + * + * The accepted units for fixed intervals are: + * millseconds (ms), seconds (s), minutes (m), hours (h) and days (d). + * @returns {DateHistogramAggregation} returns `this` so that calls can be chained + */ + fixedInterval(interval) { + this._aggsDef.fixed_interval = interval; + return this; + } } module.exports = DateHistogramAggregation; diff --git a/src/index.d.ts b/src/index.d.ts index 215a22a..1d1857d 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -5019,6 +5019,32 @@ declare namespace esb { * an identifier used in the TZ database like America/Los_Angeles. */ timeZone(tz: string): this; + + /** + * Calendar-aware intervals are configured with the calendarInterval parameter. + * The combined interval field for date histograms is deprecated from ES 7.2. + * + * @param {string} interval Interval to generate histogram over. + * You can specify calendar intervals using the unit name, such as month, or as + * a single unit quantity, such as 1M. For example, day and 1d are equivalent. + * Multiple quantities, such as 2d, are not supported. + */ + calendarInterval(interval: string): this; + + /** + * Fixed intervals are configured with the fixedInterval parameter. + * The combined interval field for date histograms is deprecated from ES 7.2. + * + * @param {string} interval Interval to generate histogram over. + * Intervals are a fixed number of SI units and never deviate, regardless + * of where they fall on the calendar. However, it means fixed intervals + * cannot express other units such as months, since the duration of a + * month is not a fixed quantity. + * + * The accepted units for fixed intervals are: + * millseconds (ms), seconds (s), minutes (m), hours (h) and days (d). + */ + fixedInterval(interval: string): this; } /** diff --git a/test/aggregations-test/date-histogram-agg.test.js b/test/aggregations-test/date-histogram-agg.test.js index 0982c9c..3ca66cb 100644 --- a/test/aggregations-test/date-histogram-agg.test.js +++ b/test/aggregations-test/date-histogram-agg.test.js @@ -36,3 +36,33 @@ test('time_zone is set', t => { }; t.deepEqual(value, expected); }); + +test('calendar_interval_is_set', t => { + const value = new DateHistogramAggregation('by_day', 'date') + .calendarInterval('month') + .toJSON(); + const expected = { + by_day: { + date_histogram: { + field: 'date', + calendar_interval: 'month' + } + } + }; + t.deepEqual(value, expected); +}); + +test('fixed_interval_is_set', t => { + const value = new DateHistogramAggregation('by_day', 'date') + .fixedInterval('90s') + .toJSON(); + const expected = { + by_day: { + date_histogram: { + field: 'date', + fixed_interval: '90s' + } + } + }; + t.deepEqual(value, expected); +});