Skip to content

Commit

Permalink
Added ValueCountAggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
mattweber committed Mar 17, 2014
1 parent 01a7bb2 commit 9419396
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 4 deletions.
57 changes: 57 additions & 0 deletions dist/elastic.js
Original file line number Diff line number Diff line change
Expand Up @@ -4529,6 +4529,63 @@
});
};

/**
@class
<p>A single-value metrics aggregation that counts the number of values that
are extracted from the aggregated documents. These values can be extracted
either from specific fields in the documents, or be generated by a provided
script. Typically, this aggregator will be used in conjunction with other
single-value aggregations.</p>

@name ejs.ValueCountAggregation
@ejs aggregation
@borrows ejs.MetricsAggregationMixin.field as field
@borrows ejs.MetricsAggregationMixin.script as script
@borrows ejs.MetricsAggregationMixin.lang as lang
@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 counts the number of values that are extracted from the
aggregated documents.</p>

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

*/
ejs.ValueCountAggregation = function (name) {

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

// not supported in value count aggregation
delete _common.scriptValuesSorted;

return extend(_common, {

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

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

agg[name].value_count.script_values_unique = trueFalse;
return this;
}

});

};

/**
@class
A container Filter that allows Boolean AND composition of Filters.
Expand Down
6 changes: 3 additions & 3 deletions dist/elastic.min.js

Large diffs are not rendered by default.

56 changes: 56 additions & 0 deletions src/aggregations/ValueCountAggregation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
@class
<p>A single-value metrics aggregation that counts the number of values that
are extracted from the aggregated documents. These values can be extracted
either from specific fields in the documents, or be generated by a provided
script. Typically, this aggregator will be used in conjunction with other
single-value aggregations.</p>
@name ejs.ValueCountAggregation
@ejs aggregation
@borrows ejs.MetricsAggregationMixin.field as field
@borrows ejs.MetricsAggregationMixin.script as script
@borrows ejs.MetricsAggregationMixin.lang as lang
@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 counts the number of values that are extracted from the
aggregated documents.</p>
@param {String} name The name which be used to refer to this aggregation.
*/
ejs.ValueCountAggregation = function (name) {

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

// not supported in value count aggregation
delete _common.scriptValuesSorted;

return extend(_common, {

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

agg[name].value_count.script_values_unique = trueFalse;
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(16);
test.expect(17);

test.ok(ejs.GlobalAggregation, 'GlobalAggregation');
test.ok(ejs.FilterAggregation, 'FilterAggregation');
Expand All @@ -46,6 +46,57 @@ exports.aggregations = {
test.ok(ejs.PercentilesAggregation, 'PercentilesAggregation');
test.ok(ejs.StatsAggregation, 'StatsAggregation');
test.ok(ejs.SumAggregation, 'SumAggregation');
test.ok(ejs.ValueCountAggregation, 'ValueCountAggregation');

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

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

expected = {
myagg: {value_count: {}}
};

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

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

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

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

agg.scriptValuesUnique(false);
expected.myagg.value_count.script_values_unique = false;
doTest();

agg.params({p1: 'v1'});
expected.myagg.value_count.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 9419396

Please sign in to comment.