elastic-builder
Version:
A JavaScript implementation of the elasticsearch Query DSL
148 lines (123 loc) • 5.32 kB
JavaScript
;
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _inherits2 = require('babel-runtime/helpers/inherits');
var _inherits3 = _interopRequireDefault(_inherits2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var isNil = require('lodash.isnil');
var PipelineAggregationBase = require('./pipeline-aggregation-base');
var ES_REF_URL = 'https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-movfn-aggregation.html';
/**
* Given an ordered series of data, the Moving Function aggregation
* will slide a window across the data and allow the user to specify
* a custom script that is executed on each window of data.
* For convenience, a number of common functions are predefined such as min/max, moving averages, etc.
*
* `moving_fn` aggregations must be embedded inside of a histogram or
* date_histogram aggregation.
*
* [Elasticsearch reference](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-movfn-aggregation.html)
*
* NOTE: Only available in Elasticsearch 6.4.0+.
*
* @example
* const agg = esb.movingFunctionAggregation('the_movfn', 'the_sum')
* .model('holt')
* .window(5)
* .gapPolicy('insert_zeros')
* .settings({ alpha: 0.8 });
*
* @example
* const reqBody = esb.requestBodySearch()
* .agg(
* esb.dateHistogramAggregation('my_date_histo', 'timestamp')
* .interval('day')
* .agg(esb.sumAggregation('the_sum', 'lemmings'))
* // Relative path to sibling metric `the_sum`
* .agg(esb.movingFunctionAggregation('the_movfn', 'the_sum'))
* )
* .size(0);
*
* @example
* const reqBody = esb.requestBodySearch()
* .agg(
* esb.dateHistogramAggregation('my_date_histo', 'timestamp')
* .interval('day')
* // Use the document count as it's input
* .agg(esb.movingFunctionAggregation('the_movfn', '_count'))
* )
* .size(0);
*
* @param {string} name The name which will be used to refer to this aggregation.
* @param {string=} bucketsPath The relative path of metric to aggregate over.
* @param {string=} window The size of window to "slide" across the histogram.
* @param {string=} script The script that should be executed on each window of data.
*
* @extends PipelineAggregationBase
*/
var MovingFunctionAggregation = function (_PipelineAggregationB) {
(0, _inherits3.default)(MovingFunctionAggregation, _PipelineAggregationB);
// eslint-disable-next-line require-jsdoc
function MovingFunctionAggregation(name, bucketsPath, window, script) {
(0, _classCallCheck3.default)(this, MovingFunctionAggregation);
var _this = (0, _possibleConstructorReturn3.default)(this, (MovingFunctionAggregation.__proto__ || Object.getPrototypeOf(MovingFunctionAggregation)).call(this, name, 'moving_fn', ES_REF_URL, bucketsPath));
if (!isNil(window)) _this._aggsDef.window = window;
if (!isNil(script)) _this._aggsDef.script = script;
return _this;
}
/**
* Sets the size of window to "slide" across the histogram. Optional.
*
* @example
* const agg = esb.movingFunctionAggregation('the_movfn', 'the_sum')
* .window(30)
*
* @param {number} window Default is 5
* @returns {MovingFunctionAggregation} returns `this` so that calls can be chained
*/
(0, _createClass3.default)(MovingFunctionAggregation, [{
key: 'window',
value: function window(_window) {
this._aggsDef.window = _window;
return this;
}
/**
* Sets shift of window position. Optional.
*
* @example
* const agg = esb.movingFunctionAggregation('the_movfn', 'the_sum')
* .shift(30)
*
* @param {number} shift Default is 0
* @returns {MovingFunctionAggregation} returns `this` so that calls can be chained
*/
}, {
key: 'shift',
value: function shift(_shift) {
this._aggsDef.shift = _shift;
return this;
}
/**
* Sets the script that should be executed on each window of data. Required.
*
* @example
* const agg = esb.movingFunctionAggregation('the_movfn', 'the_sum', "MovingFunctions.unweightedAvg(values)"")
* .script("MovingFunctions.unweightedAvg(values)")
*
* @param {string} script
* @returns {MovingFunctionAggregation} returns `this` so that calls can be chained
*/
}, {
key: 'script',
value: function script(_script) {
this._aggsDef.script = _script;
return this;
}
}]);
return MovingFunctionAggregation;
}(PipelineAggregationBase);
module.exports = MovingFunctionAggregation;