Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the geo_centroid aggregation #1150

Merged
merged 5 commits into from Jul 27, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -9,7 +9,7 @@ All notable changes to this project will be documented in this file based on the
### Bugfixes

### Added

- `Elastica\Aggregations\GeoCentroid`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a newline afterwards?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like your merge now caused it to be added twice. This one should be removed.

### Improvements
- `Elastica\Exception\InvalidException` will be thrown if you try using an
`Elastica\Aggregation\AbstractSimpleAggregation` without setting either the
Expand Down
35 changes: 35 additions & 0 deletions lib/Elastica/Aggregation/GeoCentroid.php
@@ -0,0 +1,35 @@
<?php

namespace Elastica\Aggregation;

use Elastica\Exception\InvalidException;

/**
* Class GeoCentroid.
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-geocentroid-aggregation.html
*/
class GeoCentroid extends AbstractAggregation
{
/**
* @param string $name the name of this aggregation
* @param string $field the field on which to perform this aggregation
*/
public function __construct($name, $field)
{
parent::__construct($name);
$this->setField($field);
}

/**
* Set the field for this aggregation.
*
* @param string $field the name of the document field on which to perform this aggregation
*
* @return $this
*/
public function setField($field)
{
return $this->setParam('field', $field);
}
}
45 changes: 45 additions & 0 deletions test/lib/Elastica/Test/Aggregation/GeoCentroidTest.php
@@ -0,0 +1,45 @@
<?php
namespace Elastica\Test\Aggregation;

use Elastica\Aggregation\GeoCentroid;
use Elastica\Document;
use Elastica\Query;
use Elastica\Type\Mapping;

class GeoCentroidTest extends BaseAggregationTest
{
protected function _getIndexForTest()
{
$index = $this->_createIndex();
$type = $index->getType('test');

$type->setMapping(new Mapping(null, array(
'location' => array('type' => 'geo_point'),
)));

$type->addDocuments(array(
new Document(1, array('location' => array('lat' => 32.849437, 'lon' => -117.271732))),
new Document(2, array('location' => array('lat' => 32.798320, 'lon' => -117.246648))),
new Document(3, array('location' => array('lat' => 37.782439, 'lon' => -122.392560))),
));

$index->refresh();

return $index;
}

/**
* @group functional
*/
public function testGeohashGridAggregation()
{
$agg = new GeoCentroid('centroid', 'location');

$query = new Query();
$query->addAggregation($agg);
$results = $this->_getIndexForTest()->search($query)->getAggregation('centroid');

$this->assertEquals(34.476731875911, $results['location']['lat']);
$this->assertEquals(-118.97031342611, $results['location']['lon']);
}
}