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 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file based on the

### Added
- Elastica\QueryBuilder\DSL\Query::geo_distance
- Elastica\Aggregation\GeoCentroid
Copy link
Owner

Choose a reason for hiding this comment

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

If you already edit the changelog, you could also add #1150 at the end (see change examples below)


### Improvements
- Set PHP 7.0 as default development version
Expand Down Expand Up @@ -38,6 +39,7 @@ All notable changes to this project will be documented in this file based on the
- Fix php notice on `\Elastica\Index::getAliases()` if index has no aliases [#1078](https://github.com/ruflin/Elastica/issues/1078)

### 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.

- Update elasticsearch build dependency to elasticsearch 2.3.2 [#1084](https://github.com/ruflin/Elastica/pull/1084)

### Improvements
Expand Down
35 changes: 35 additions & 0 deletions lib/Elastica/Aggregation/GeoCentroid.php
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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']);
}
}