Skip to content
Snippets Groups Projects
Commit 504c5560 authored by Simonas Šerlinskas's avatar Simonas Šerlinskas Committed by GitHub
Browse files

Merge pull request #109 from einorler/geo_centroid_agg

added geo centroid aggregation
parents 8d749690 69fc6666
No related branches found
No related tags found
No related merge requests found
# Geo Centroid Aggregation
> More info about histogram aggregation is in the [official elasticsearch docs][1]
A metric aggregation that computes the weighted centroid from all coordinate values for a Geo-point datatype field.
The data type of the field that is specified for the aggregation must be `geo-point`.
## Simple example
```JSON
{
"query" : {
"match" : { "crime" : "burglary" }
},
"aggs" : {
"centroid" : {
"geo_centroid" : {
"field" : "location"
}
}
}
}
```
And now the query via DSL:
```php
$geoCentroidAggregation = new GeoCentroidAggregation('centroid', 'location');
$search = new Search();
$search->addQuery(new MatchQuery('crime', 'burglary'));
$search->addAggregation($geoCentroidAggregation);
$queryArray = $search->toArray();
```
## Advanced example
The query provides more information when when combined as a sub-aggregation to other bucket aggregations. Here is an example of that:
```JSON
{
"query" : {
"match" : { "crime" : "burglary" }
},
"aggs" : {
"towns" : {
"terms" : { "field" : "town" },
"aggs" : {
"centroid" : {
"geo_centroid" : { "field" : "location" }
}
}
}
}
}
```
And now via DSL:
```php
$geoCentroidAggregation = new GeoCentroidAggregation('centroid', 'location');
$termsAggregation = new TermsAggregation('towns', 'town');
$termsAggregation->addAggregation($geoCentroidAggregation);
$search = new Search();
$search->addQuery(new MatchQuery('crime', 'burglary'));
$search->addAggregation($termsAggregation);
$queryArray = $search->toArray();
```
[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-geocentroid-aggregation.html
<?php
/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ONGR\ElasticsearchDSL\Aggregation;
use ONGR\ElasticsearchDSL\Aggregation\Type\MetricTrait;
/**
* Class representing geo centroid aggregation.
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-geocentroid-aggregation.html
*/
class GeoCentroidAggregation extends AbstractAggregation
{
use MetricTrait;
/**
* Inner aggregations container init.
*
* @param string $name
* @param string $field
*/
public function __construct($name, $field = null)
{
parent::__construct($name);
$this->setField($field);
}
/**
* {@inheritdoc}
*/
public function getArray()
{
$data = [];
if ($this->getField()) {
$data['field'] = $this->getField();
} else {
throw new \LogicException('Geo centroid aggregation must have a field set.');
}
return $data;
}
/**
* {@inheritdoc}
*/
public function getType()
{
return 'geo_centroid';
}
}
<?php
/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ONGR\ElasticsearchDSL\Tests\Aggregation;
use ONGR\ElasticsearchDSL\Aggregation\GeoCentroidAggregation;
/**
* Unit test for children aggregation.
*/
class GeoCentroidAggregationTest extends \PHPUnit_Framework_TestCase
{
/**
* Test if exception is thrown when field is not provided
*
* @expectedException \LogicException
*/
public function testGetArrayException()
{
$aggregation = new GeoCentroidAggregation('foo');
$aggregation->getArray();
}
/**
* Tests getType method.
*/
public function testGeoCentroidAggregationGetType()
{
$aggregation = new GeoCentroidAggregation('foo');
$this->assertEquals('geo_centroid', $aggregation->getType());
}
/**
* Tests getArray method.
*/
public function testGeoCentroidAggregationGetArray()
{
$aggregation = new GeoCentroidAggregation('foo', 'location');
$this->assertEquals(['field' => 'location'], $aggregation->getArray());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment