Skip to content

Commit

Permalink
feat: Add calendar_interval, fixed_interval to date-histogram agg (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andi Pieper authored Nov 22, 2020
1 parent 5f82d92 commit 46b7d79
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/aggregations/bucket-aggregations/date-histogram-aggregation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
26 changes: 26 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
30 changes: 30 additions & 0 deletions test/aggregations-test/date-histogram-agg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit 46b7d79

Please sign in to comment.