Skip to content

Commit 62fafe0

Browse files
coreationruflin
authored andcommitted
Added the geo_centroid aggregation (#1150)
1 parent a25b6c7 commit 62fafe0

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file based on the
1010

1111
### Added
1212
- Elastica\QueryBuilder\DSL\Query::geo_distance
13+
- Elastica\Aggregation\GeoCentroid [#1150](https://github.com/ruflin/Elastica/pull/1150)
1314

1415
### Improvements
1516
- Set PHP 7.0 as default development version
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Elastica\Aggregation;
4+
5+
use Elastica\Exception\InvalidException;
6+
7+
/**
8+
* Class GeoCentroid.
9+
*
10+
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-geocentroid-aggregation.html
11+
*/
12+
class GeoCentroid extends AbstractAggregation
13+
{
14+
/**
15+
* @param string $name the name of this aggregation
16+
* @param string $field the field on which to perform this aggregation
17+
*/
18+
public function __construct($name, $field)
19+
{
20+
parent::__construct($name);
21+
$this->setField($field);
22+
}
23+
24+
/**
25+
* Set the field for this aggregation.
26+
*
27+
* @param string $field the name of the document field on which to perform this aggregation
28+
*
29+
* @return $this
30+
*/
31+
public function setField($field)
32+
{
33+
return $this->setParam('field', $field);
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
namespace Elastica\Test\Aggregation;
3+
4+
use Elastica\Aggregation\GeoCentroid;
5+
use Elastica\Document;
6+
use Elastica\Query;
7+
use Elastica\Type\Mapping;
8+
9+
class GeoCentroidTest extends BaseAggregationTest
10+
{
11+
protected function _getIndexForTest()
12+
{
13+
$index = $this->_createIndex();
14+
$type = $index->getType('test');
15+
16+
$type->setMapping(new Mapping(null, array(
17+
'location' => array('type' => 'geo_point'),
18+
)));
19+
20+
$type->addDocuments(array(
21+
new Document(1, array('location' => array('lat' => 32.849437, 'lon' => -117.271732))),
22+
new Document(2, array('location' => array('lat' => 32.798320, 'lon' => -117.246648))),
23+
new Document(3, array('location' => array('lat' => 37.782439, 'lon' => -122.392560))),
24+
));
25+
26+
$index->refresh();
27+
28+
return $index;
29+
}
30+
31+
/**
32+
* @group functional
33+
*/
34+
public function testGeohashGridAggregation()
35+
{
36+
$agg = new GeoCentroid('centroid', 'location');
37+
38+
$query = new Query();
39+
$query->addAggregation($agg);
40+
$results = $this->_getIndexForTest()->search($query)->getAggregation('centroid');
41+
42+
$this->assertEquals(34.476731875911, $results['location']['lat']);
43+
$this->assertEquals(-118.97031342611, $results['location']['lon']);
44+
}
45+
}

0 commit comments

Comments
 (0)