Skip to content

Commit

Permalink
Add GeoHashGridAggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
mattweber committed Mar 17, 2014
1 parent 41a834e commit 90cd2ce
Show file tree
Hide file tree
Showing 4 changed files with 253 additions and 9 deletions.
101 changes: 101 additions & 0 deletions dist/elastic.js
Original file line number Diff line number Diff line change
Expand Up @@ -2937,6 +2937,107 @@
});
};

/**
@class
<p>A multi-bucket aggregation that works on geo_point fields and groups points
into buckets that represent cells in a grid. The resulting grid can be sparse
and only contains cells that have matching data. Each cell is labeled using a
geohash which is of user-definable precision.</p>
@name ejs.GeoHashGridAggregation
@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
@desc
<p>Aggregation that works on geo_point fields and groups points into buckets
that represent cells in a grid.</p>
@param {String} name The name which be used to refer to this aggregation.
*/
ejs.GeoHashGridAggregation = function (name) {

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

agg[name].geohash_grid = {};

return extend(_common, {

/**
Sets the geo field to perform calculations from.
@member ejs.GeoHashGridAggregation
@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].geohash_grid.field;
}

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

/**
Sets the Geo Hash precision. The precision value can be between 1 and 12
where 12 is the highest precision.
@member ejs.GeoHashGridAggregation
@param {Integer} p The precision. Integer between 1 and 12.
@returns {Object} returns <code>this</code> so that calls can be chained.
*/
precision: function (p) {
if (p == null) {
return agg[name].geohash_grid.precision;
}

agg[name].geohash_grid.precision = p;
return this;
},

/**
Sets the number of aggregation entries that will be returned.
@member ejs.GeoHashGridAggregation
@param {Integer} size The numer of aggregation entries to be returned.
@returns {Object} returns <code>this</code> so that calls can be chained.
*/
size: function (size) {
if (size == null) {
return agg[name].geohash_grid.size;
}

agg[name].geohash_grid.size = size;
return this;
},


/**
Determines how many geohash_grid the coordinating node will request from
each shard.
@member ejs.GeoHashGridAggregation
@param {Integer} shardSize The numer of geohash_grid to fetch from each shard.
@returns {Object} returns <code>this</code> so that calls can be chained.
*/
shardSize: function (shardSize) {
if (shardSize == null) {
return agg[name].geohash_grid.shard_size;
}

agg[name].geohash_grid.shard_size = shardSize;
return this;
}

});
};

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

Large diffs are not rendered by default.

100 changes: 100 additions & 0 deletions src/aggregations/GeoHashGridAggregation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
@class
<p>A multi-bucket aggregation that works on geo_point fields and groups points
into buckets that represent cells in a grid. The resulting grid can be sparse
and only contains cells that have matching data. Each cell is labeled using a
geohash which is of user-definable precision.</p>
@name ejs.GeoHashGridAggregation
@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
@desc
<p>Aggregation that works on geo_point fields and groups points into buckets
that represent cells in a grid.</p>
@param {String} name The name which be used to refer to this aggregation.
*/
ejs.GeoHashGridAggregation = function (name) {

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

agg[name].geohash_grid = {};

return extend(_common, {

/**
Sets the geo field to perform calculations from.
@member ejs.GeoHashGridAggregation
@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].geohash_grid.field;
}

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

/**
Sets the Geo Hash precision. The precision value can be between 1 and 12
where 12 is the highest precision.
@member ejs.GeoHashGridAggregation
@param {Integer} p The precision. Integer between 1 and 12.
@returns {Object} returns <code>this</code> so that calls can be chained.
*/
precision: function (p) {
if (p == null) {
return agg[name].geohash_grid.precision;
}

agg[name].geohash_grid.precision = p;
return this;
},

/**
Sets the number of aggregation entries that will be returned.
@member ejs.GeoHashGridAggregation
@param {Integer} size The numer of aggregation entries to be returned.
@returns {Object} returns <code>this</code> so that calls can be chained.
*/
size: function (size) {
if (size == null) {
return agg[name].geohash_grid.size;
}

agg[name].geohash_grid.size = size;
return this;
},


/**
Determines how many geohash_grid the coordinating node will request from
each shard.
@member ejs.GeoHashGridAggregation
@param {Integer} shardSize The numer of geohash_grid to fetch from each shard.
@returns {Object} returns <code>this</code> so that calls can be chained.
*/
shardSize: function (shardSize) {
if (shardSize == null) {
return agg[name].geohash_grid.shard_size;
}

agg[name].geohash_grid.shard_size = shardSize;
return this;
}

});
};
55 changes: 49 additions & 6 deletions tests/aggregation_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,63 @@ exports.aggregations = {
done();
},
exists: function (test) {
test.expect(3);
test.expect(4);

test.ok(ejs.GlobalAggregation, 'GlobalAggregation');
test.ok(ejs.FilterAggregation, 'FilterAggregation');
test.ok(ejs.TermsAggregation, 'TermsAggregation');
test.ok(ejs.GeoHashGridAggregation, 'GeoHashGridAggregation');

test.done();
},
GeoHashGridAggregation: function (test) {
test.expect(10);

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

expected = {
myagg: {geohash_grid: {}}
};

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

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

agg.precision(6);
expected.myagg.geohash_grid.precision = 6;
doTest();

agg.size(10);
expected.myagg.geohash_grid.size = 10;
doTest();

agg.shardSize(100);
expected.myagg.geohash_grid.shard_size = 100;
doTest();

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

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

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

test.done();
},
TermsAggregation: function (test) {
test.expect(33);
test.expect(32);

var agg = ejs.TermsAggregation('myagg'),
ta1 = ejs.TermsAggregation('ta1').field('f1'),
Expand Down Expand Up @@ -166,10 +213,6 @@ exports.aggregations = {
agg.agggregation('invalid');
}, TypeError);

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

test.done();
},
FilterAggregation: function (test) {
Expand Down

0 comments on commit 90cd2ce

Please sign in to comment.