diff --git a/docs/Aggregation/GeoCentroid.md b/docs/Aggregation/GeoCentroid.md new file mode 100644 index 0000000000000000000000000000000000000000..7925b31881cb3cb0bdb2f0c20c9cd39b4653b502 --- /dev/null +++ b/docs/Aggregation/GeoCentroid.md @@ -0,0 +1,74 @@ +# 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 diff --git a/src/Aggregation/GeoCentroidAggregation.php b/src/Aggregation/GeoCentroidAggregation.php new file mode 100644 index 0000000000000000000000000000000000000000..51399c4d4637fc35a42322ee5f4206ae42d42d45 --- /dev/null +++ b/src/Aggregation/GeoCentroidAggregation.php @@ -0,0 +1,60 @@ +<?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'; + } +} diff --git a/tests/Aggregation/GeoCentroidAggregationTest.php b/tests/Aggregation/GeoCentroidAggregationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..e8d6ea9f52e66db4ae8253155f8447169a8c8c51 --- /dev/null +++ b/tests/Aggregation/GeoCentroidAggregationTest.php @@ -0,0 +1,49 @@ +<?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()); + } +}