Skip to content

Commit

Permalink
Add MetricsAggregationMixin and AvgAggregation.
Browse files Browse the repository at this point in the history
The Metrics Aggregation Mixin can be used as the base
for most metrics based aggregations.  The AvgAggregation
uses the metrics aggregation mixin for all methods.
  • Loading branch information
mattweber committed Mar 17, 2014
1 parent bdd6acd commit 1f26194
Show file tree
Hide file tree
Showing 5 changed files with 341 additions and 4 deletions.
144 changes: 144 additions & 0 deletions dist/elastic.js
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,115 @@
};
};

/**
@mixin
<p>The MetricsAggregationMixin provides support for common options used across
various metrics <code>Aggregation</code> implementations. This object should
not be used directly.</p>

@name ejs.MetricsAggregationMixin
@ejs aggregation
@borrows ejs.AggregationMixin.aggregation as aggregation
@borrows ejs.AggregationMixin.agg as agg
@borrows ejs.AggregationMixin._type as _type
@borrows ejs.AggregationMixin.toJSON as toJSON

*/
ejs.MetricsAggregationMixin = function (name, type) {

var
_common = ejs.AggregationMixin(name),
agg = _common.toJSON();

agg[name][type] = {};

return extend(_common, {

/**
<p>Sets the field to operate on.</p>

@member ejs.MetricsAggregationMixin
@param {String} field a valid field name..
@returns {Object} returns <code>this</code> so that calls can be chained.
*/
field: function (field) {
if (field == null) {
return agg[name][type].field;
}

agg[name][type].field = field;
return this;
},

/**
Allows you generate or modify the terms/values using a script.

@member ejs.MetricsAggregationMixin
@param {String} scriptCode A valid script string to execute.
@returns {Object} returns <code>this</code> so that calls can be chained.
*/
script: function (scriptCode) {
if (scriptCode == null) {
return agg[name][type].script;
}

agg[name][type].script = scriptCode;
return this;
},

/**
The script language being used.

@member ejs.MetricsAggregationMixin
@param {String} language The language of the script.
@returns {Object} returns <code>this</code> so that calls can be chained.
*/
lang: function (language) {
if (language == null) {
return agg[name][type].lang;
}

agg[name][type].lang = language;
return this;
},

/**
Set to true to assume script values are sorted.

@member ejs.MetricsAggregationMixin
@param {Boolean} trueFalse assume sorted values or not
@returns {Object} returns <code>this</code> so that calls can be chained.
*/
scriptValuesSorted: function (trueFalse) {
if (trueFalse == null) {
return agg[name][type].script_values_sorted;
}

agg[name][type].script_values_sorted = trueFalse;
return this;
},

/**
Sets parameters that will be applied to the script. Overwrites
any existing params.

@member ejs.MetricsAggregationMixin
@param {Object} p An object where the keys are the parameter name and
values are the parameter value.
@returns {Object} returns <code>this</code> so that calls can be chained.
*/
params: function (p) {
if (p == null) {
return agg[name][type].params;
}

agg[name][type].params = p;
return this;
}

});
};

/**
@mixin
<p>The QueryMixin provides support for common options used across
Expand Down Expand Up @@ -2887,6 +2996,41 @@
});
};

/**
@class
<p>A single-value metrics aggregation that computes the average of numeric
values that are extracted from the aggregated documents. These values can be
extracted either from specific numeric fields in the documents, or be
generated by a provided script.</p>

@name ejs.AvgAggregation
@ejs aggregation
@borrows ejs.MetricsAggregationMixin.field as field
@borrows ejs.MetricsAggregationMixin.script as script
@borrows ejs.MetricsAggregationMixin.lang as lang
@borrows ejs.MetricsAggregationMixin.scriptValuesSorted as scriptValuesSorted
@borrows ejs.MetricsAggregationMixin.params as params
@borrows ejs.MetricsAggregationMixin.aggregation as aggregation
@borrows ejs.MetricsAggregationMixin.agg as agg
@borrows ejs.MetricsAggregationMixin._type as _type
@borrows ejs.MetricsAggregationMixin.toJSON as toJSON

@desc
<p>Aggregation that computes the average of numeric values that are extracted
from the aggregated documents.</p>

@param {String} name The name which be used to refer to this aggregation.

*/
ejs.AvgAggregation = function (name) {

var
_common = ejs.MetricsAggregationMixin(name, 'avg'),
agg = _common.toJSON();

return _common;
};

/**
@class
<p>Defines a single bucket of all the documents in the current document set
Expand Down
6 changes: 3 additions & 3 deletions dist/elastic.min.js

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions src/aggregations/AvgAggregation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
@class
<p>A single-value metrics aggregation that computes the average of numeric
values that are extracted from the aggregated documents. These values can be
extracted either from specific numeric fields in the documents, or be
generated by a provided script.</p>
@name ejs.AvgAggregation
@ejs aggregation
@borrows ejs.MetricsAggregationMixin.field as field
@borrows ejs.MetricsAggregationMixin.script as script
@borrows ejs.MetricsAggregationMixin.lang as lang
@borrows ejs.MetricsAggregationMixin.scriptValuesSorted as scriptValuesSorted
@borrows ejs.MetricsAggregationMixin.params as params
@borrows ejs.MetricsAggregationMixin.aggregation as aggregation
@borrows ejs.MetricsAggregationMixin.agg as agg
@borrows ejs.MetricsAggregationMixin._type as _type
@borrows ejs.MetricsAggregationMixin.toJSON as toJSON
@desc
<p>Aggregation that computes the average of numeric values that are extracted
from the aggregated documents.</p>
@param {String} name The name which be used to refer to this aggregation.
*/
ejs.AvgAggregation = function (name) {

var
_common = ejs.MetricsAggregationMixin(name, 'avg'),
agg = _common.toJSON();

return _common;
};
108 changes: 108 additions & 0 deletions src/mixins/MetricsAggregationMixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
@mixin
<p>The MetricsAggregationMixin provides support for common options used across
various metrics <code>Aggregation</code> implementations. This object should
not be used directly.</p>
@name ejs.MetricsAggregationMixin
@ejs aggregation
@borrows ejs.AggregationMixin.aggregation as aggregation
@borrows ejs.AggregationMixin.agg as agg
@borrows ejs.AggregationMixin._type as _type
@borrows ejs.AggregationMixin.toJSON as toJSON
*/
ejs.MetricsAggregationMixin = function (name, type) {

var
_common = ejs.AggregationMixin(name),
agg = _common.toJSON();

agg[name][type] = {};

return extend(_common, {

/**
<p>Sets the field to operate on.</p>
@member ejs.MetricsAggregationMixin
@param {String} field a valid field name..
@returns {Object} returns <code>this</code> so that calls can be chained.
*/
field: function (field) {
if (field == null) {
return agg[name][type].field;
}

agg[name][type].field = field;
return this;
},

/**
Allows you generate or modify the terms/values using a script.
@member ejs.MetricsAggregationMixin
@param {String} scriptCode A valid script string to execute.
@returns {Object} returns <code>this</code> so that calls can be chained.
*/
script: function (scriptCode) {
if (scriptCode == null) {
return agg[name][type].script;
}

agg[name][type].script = scriptCode;
return this;
},

/**
The script language being used.
@member ejs.MetricsAggregationMixin
@param {String} language The language of the script.
@returns {Object} returns <code>this</code> so that calls can be chained.
*/
lang: function (language) {
if (language == null) {
return agg[name][type].lang;
}

agg[name][type].lang = language;
return this;
},

/**
Set to true to assume script values are sorted.
@member ejs.MetricsAggregationMixin
@param {Boolean} trueFalse assume sorted values or not
@returns {Object} returns <code>this</code> so that calls can be chained.
*/
scriptValuesSorted: function (trueFalse) {
if (trueFalse == null) {
return agg[name][type].script_values_sorted;
}

agg[name][type].script_values_sorted = trueFalse;
return this;
},

/**
Sets parameters that will be applied to the script. Overwrites
any existing params.
@member ejs.MetricsAggregationMixin
@param {Object} p An object where the keys are the parameter name and
values are the parameter value.
@returns {Object} returns <code>this</code> so that calls can be chained.
*/
params: function (p) {
if (p == null) {
return agg[name][type].params;
}

agg[name][type].params = p;
return this;
}

});
};
53 changes: 52 additions & 1 deletion tests/aggregation_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ exports.aggregations = {
done();
},
exists: function (test) {
test.expect(9);
test.expect(10);

test.ok(ejs.GlobalAggregation, 'GlobalAggregation');
test.ok(ejs.FilterAggregation, 'FilterAggregation');
Expand All @@ -39,6 +39,57 @@ exports.aggregations = {
test.ok(ejs.NestedAggregation, 'NestedAggregation');
test.ok(ejs.RangeAggregation, 'RangeAggregation');
test.ok(ejs.SignificantTermsAggregation, 'SignificantTermsAggregation');
test.ok(ejs.AvgAggregation, 'AvgAggregation');

test.done();
},
AvgAggregation: function (test) {
test.expect(11);

var agg = ejs.AvgAggregation('myagg'),
ta1 = ejs.TermsAggregation('ta1').field('f1'),
expected,
doTest = function () {
test.deepEqual(agg.toJSON(), expected);
};

expected = {
myagg: {avg: {}}
};

test.ok(agg, 'AvgAggregation exists');
test.ok(agg.toJSON(), 'toJSON() works');
doTest();

agg.field('f1');
expected.myagg.avg.field = 'f1';
doTest();

agg.script('s1');
expected.myagg.avg.script = 's1';
doTest();

agg.lang('mvel');
expected.myagg.avg.lang = 'mvel';
doTest();

agg.scriptValuesSorted(false);
expected.myagg.avg.script_values_sorted = false;
doTest();

agg.params({p1: 'v1'});
expected.myagg.avg.params = {p1: 'v1'};
doTest();

agg.agg(ta1);
expected.myagg.aggs = ta1.toJSON();
doTest();

test.strictEqual(agg._type(), 'aggregation');

test.throws(function () {
agg.agggregation('invalid');
}, TypeError);

test.done();
},
Expand Down

0 comments on commit 1f26194

Please sign in to comment.