From dca0ca9ff6a49be98373af8015213a24489f18b4 Mon Sep 17 00:00:00 2001 From: Anna Krawet Date: Tue, 1 Dec 2020 11:10:38 +0100 Subject: [PATCH 1/3] added geotile grid support --- src/Aggregation/GeotileGrid.php | 70 ++++++++++++++++++++++ src/QueryBuilder/DSL/Aggregation.php | 14 +++++ tests/Aggregation/GeoCentroidTest.php | 2 +- tests/Aggregation/GeotileGridTest.php | 49 +++++++++++++++ tests/QueryBuilder/DSL/AggregationTest.php | 1 + 5 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 src/Aggregation/GeotileGrid.php create mode 100644 tests/Aggregation/GeotileGridTest.php diff --git a/src/Aggregation/GeotileGrid.php b/src/Aggregation/GeotileGrid.php new file mode 100644 index 0000000000..4c80609fed --- /dev/null +++ b/src/Aggregation/GeotileGrid.php @@ -0,0 +1,70 @@ +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(string $field): self + { + return $this->setParam('field', $field); + } + + /** + * Set the precision for this aggregation. + * + * @param int $precision an integer between 1 and 12, inclusive. Defaults to 5. + * + * @return $this + */ + public function setPrecision(int $precision): self + { + return $this->setParam('precision', $precision); + } + + /** + * Set the maximum number of buckets to return. + * + * @param int $size defaults to 10,000 + * + * @return $this + */ + public function setSize(int $size): self + { + return $this->setParam('size', $size); + } + + /** + * Set the number of results returned from each shard. + * + * @return $this + */ + public function setShardSize(int $shardSize): self + { + return $this->setParam('shard_size', $shardSize); + } +} diff --git a/src/QueryBuilder/DSL/Aggregation.php b/src/QueryBuilder/DSL/Aggregation.php index 0a34a87535..3c736e64b7 100644 --- a/src/QueryBuilder/DSL/Aggregation.php +++ b/src/QueryBuilder/DSL/Aggregation.php @@ -16,6 +16,7 @@ use Elastica\Aggregation\Filters; use Elastica\Aggregation\GeoDistance; use Elastica\Aggregation\GeohashGrid; +use Elastica\Aggregation\GeotileGrid; use Elastica\Aggregation\GlobalAggregation; use Elastica\Aggregation\Histogram; use Elastica\Aggregation\IpRange; @@ -404,6 +405,19 @@ public function geohash_grid(string $name, string $field): GeohashGrid return new GeohashGrid($name, $field); } + /** + * geotile grid aggregation. + * + * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-geotilegrid-aggregation.html + * + * @param string $name the name of this aggregation + * @param string $field the field on which to perform this aggregation + */ + public function geotile_grid(string $name, string $field): GeotileGrid + { + return new GeotileGrid($name, $field); + } + /** * bucket script aggregation. * diff --git a/tests/Aggregation/GeoCentroidTest.php b/tests/Aggregation/GeoCentroidTest.php index b7220c9bc1..07405b8f03 100644 --- a/tests/Aggregation/GeoCentroidTest.php +++ b/tests/Aggregation/GeoCentroidTest.php @@ -16,7 +16,7 @@ class GeoCentroidTest extends BaseAggregationTest /** * @group functional */ - public function testGeohashGridAggregation(): void + public function testGeoCentroidGridAggregation(): void { $agg = new GeoCentroid('centroid', 'location'); diff --git a/tests/Aggregation/GeotileGridTest.php b/tests/Aggregation/GeotileGridTest.php new file mode 100644 index 0000000000..9f992a9fcf --- /dev/null +++ b/tests/Aggregation/GeotileGridTest.php @@ -0,0 +1,49 @@ +setPrecision(7); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('tile'); + + $this->assertEquals(2, $results['buckets'][0]['doc_count']); + $this->assertEquals(1, $results['buckets'][1]['doc_count']); + } + + protected function _getIndexForTest(): Index + { + $index = $this->_createIndex(); + $index->setMapping(new Mapping([ + 'location' => ['type' => 'geo_point'], + ])); + + $index->addDocuments([ + new Document(1, ['location' => ['lat' => 32.849437, 'lon' => -117.271732]]), + new Document(2, ['location' => ['lat' => 32.798320, 'lon' => -117.246648]]), + new Document(3, ['location' => ['lat' => 37.782439, 'lon' => -122.392560]]), + ]); + + $index->refresh(); + + return $index; + } +} diff --git a/tests/QueryBuilder/DSL/AggregationTest.php b/tests/QueryBuilder/DSL/AggregationTest.php index 356d5a62af..6f8d7c4f72 100644 --- a/tests/QueryBuilder/DSL/AggregationTest.php +++ b/tests/QueryBuilder/DSL/AggregationTest.php @@ -41,6 +41,7 @@ public function testInterface(): void $this->_assertImplemented($aggregationDSL, 'filters', Aggregation\Filters::class, ['name']); $this->_assertImplemented($aggregationDSL, 'geo_distance', Aggregation\GeoDistance::class, ['name', 'field', 'origin']); $this->_assertImplemented($aggregationDSL, 'geohash_grid', Aggregation\GeohashGrid::class, ['name', 'field']); + $this->_assertImplemented($aggregationDSL, 'geotile_grid', Aggregation\GeotileGrid::class, ['name', 'field']); $this->_assertImplemented($aggregationDSL, 'global', Aggregation\GlobalAggregation::class, ['name']); $this->_assertImplemented($aggregationDSL, 'histogram', Aggregation\Histogram::class, ['name', 'field', 1]); $this->_assertImplemented($aggregationDSL, 'ipv4_range', Aggregation\IpRange::class, ['name', 'field']); From faa148659e518b7556c84ee00b4112eba695b25a Mon Sep 17 00:00:00 2001 From: Anna Krawet Date: Wed, 2 Dec 2020 14:29:31 +0100 Subject: [PATCH 2/3] added geotile grid support to CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 05aa1f436e..b82435a76a 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Added `Elastica\Aggregation\GeoDistance::setKeyed()` [#1876](https://github.com/ruflin/Elastica/pull/1876) * Added `Elastica\Aggregation\Histogram::setKeyed()` [#1876](https://github.com/ruflin/Elastica/pull/1876) * Added `Elastica\Aggregation\IpRange::setKeyed()` [#1876](https://github.com/ruflin/Elastica/pull/1876) +* Added `Elastica\Aggregation\GeotileGrid` [#1880](https://github.com/ruflin/Elastica/pull/1880) ### Changed * Allow `string` such as `wait_for` to be passed to `AbstractUpdateAction::setRefresh` [#1791](https://github.com/ruflin/Elastica/pull/1791) * Changed the return type of `AbstractUpdateAction::getRefresh` to `boolean|string` [#1791](https://github.com/ruflin/Elastica/pull/1791) From 146bb1b9006693e545a3e14b625734f6063c9f16 Mon Sep 17 00:00:00 2001 From: Anna Krawet Date: Fri, 4 Dec 2020 12:51:22 +0100 Subject: [PATCH 3/3] class renamed + use of new trait --- CHANGELOG.md | 2 +- ...tileGrid.php => GeotileGridAggregation.php} | 18 +++++------------- src/QueryBuilder/DSL/Aggregation.php | 6 +++--- ...Test.php => GeotileGridAggregationTest.php} | 6 +++--- tests/QueryBuilder/DSL/AggregationTest.php | 2 +- 5 files changed, 13 insertions(+), 21 deletions(-) rename src/Aggregation/{GeotileGrid.php => GeotileGridAggregation.php} (80%) rename tests/Aggregation/{GeotileGridTest.php => GeotileGridAggregationTest.php} (87%) diff --git a/CHANGELOG.md b/CHANGELOG.md index b82435a76a..f166b3bced 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,7 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Added `Elastica\Aggregation\GeoDistance::setKeyed()` [#1876](https://github.com/ruflin/Elastica/pull/1876) * Added `Elastica\Aggregation\Histogram::setKeyed()` [#1876](https://github.com/ruflin/Elastica/pull/1876) * Added `Elastica\Aggregation\IpRange::setKeyed()` [#1876](https://github.com/ruflin/Elastica/pull/1876) -* Added `Elastica\Aggregation\GeotileGrid` [#1880](https://github.com/ruflin/Elastica/pull/1880) +* Added `Elastica\Aggregation\GeotileGridAggregation` [#1880](https://github.com/ruflin/Elastica/pull/1880) ### Changed * Allow `string` such as `wait_for` to be passed to `AbstractUpdateAction::setRefresh` [#1791](https://github.com/ruflin/Elastica/pull/1791) * Changed the return type of `AbstractUpdateAction::getRefresh` to `boolean|string` [#1791](https://github.com/ruflin/Elastica/pull/1791) diff --git a/src/Aggregation/GeotileGrid.php b/src/Aggregation/GeotileGridAggregation.php similarity index 80% rename from src/Aggregation/GeotileGrid.php rename to src/Aggregation/GeotileGridAggregation.php index 4c80609fed..aa41687313 100644 --- a/src/Aggregation/GeotileGrid.php +++ b/src/Aggregation/GeotileGridAggregation.php @@ -3,13 +3,15 @@ namespace Elastica\Aggregation; /** - * Class GeotileGrid. + * Class GeotileGridAggregation. * * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-geotilegrid-aggregation.html */ -class GeotileGrid extends AbstractAggregation +class GeotileGridAggregation extends AbstractAggregation { - public const DEFAULT_PRECISION_VALUE = 5; + use Traits\ShardSizeTrait; + + public const DEFAULT_PRECISION_VALUE = 7; public const DEFAULT_SIZE_VALUE = 10000; /** @@ -57,14 +59,4 @@ public function setSize(int $size): self { return $this->setParam('size', $size); } - - /** - * Set the number of results returned from each shard. - * - * @return $this - */ - public function setShardSize(int $shardSize): self - { - return $this->setParam('shard_size', $shardSize); - } } diff --git a/src/QueryBuilder/DSL/Aggregation.php b/src/QueryBuilder/DSL/Aggregation.php index 3c736e64b7..28b97f1cef 100644 --- a/src/QueryBuilder/DSL/Aggregation.php +++ b/src/QueryBuilder/DSL/Aggregation.php @@ -16,7 +16,7 @@ use Elastica\Aggregation\Filters; use Elastica\Aggregation\GeoDistance; use Elastica\Aggregation\GeohashGrid; -use Elastica\Aggregation\GeotileGrid; +use Elastica\Aggregation\GeotileGridAggregation; use Elastica\Aggregation\GlobalAggregation; use Elastica\Aggregation\Histogram; use Elastica\Aggregation\IpRange; @@ -413,9 +413,9 @@ public function geohash_grid(string $name, string $field): GeohashGrid * @param string $name the name of this aggregation * @param string $field the field on which to perform this aggregation */ - public function geotile_grid(string $name, string $field): GeotileGrid + public function geotile_grid(string $name, string $field): GeotileGridAggregation { - return new GeotileGrid($name, $field); + return new GeotileGridAggregation($name, $field); } /** diff --git a/tests/Aggregation/GeotileGridTest.php b/tests/Aggregation/GeotileGridAggregationTest.php similarity index 87% rename from tests/Aggregation/GeotileGridTest.php rename to tests/Aggregation/GeotileGridAggregationTest.php index 9f992a9fcf..96732a4ae2 100644 --- a/tests/Aggregation/GeotileGridTest.php +++ b/tests/Aggregation/GeotileGridAggregationTest.php @@ -2,7 +2,7 @@ namespace Elastica\Test\Aggregation; -use Elastica\Aggregation\GeotileGrid; +use Elastica\Aggregation\GeotileGridAggregation; use Elastica\Document; use Elastica\Index; use Elastica\Mapping; @@ -11,14 +11,14 @@ /** * @internal */ -class GeotileGridTest extends BaseAggregationTest +class GeotileGridAggregationTest extends BaseAggregationTest { /** * @group functional */ public function testGeotileGridAggregation(): void { - $agg = new GeotileGrid('tile', 'location'); + $agg = new GeotileGridAggregation('tile', 'location'); $agg->setPrecision(7); $query = new Query(); diff --git a/tests/QueryBuilder/DSL/AggregationTest.php b/tests/QueryBuilder/DSL/AggregationTest.php index 6f8d7c4f72..70fb8f0a55 100644 --- a/tests/QueryBuilder/DSL/AggregationTest.php +++ b/tests/QueryBuilder/DSL/AggregationTest.php @@ -41,7 +41,7 @@ public function testInterface(): void $this->_assertImplemented($aggregationDSL, 'filters', Aggregation\Filters::class, ['name']); $this->_assertImplemented($aggregationDSL, 'geo_distance', Aggregation\GeoDistance::class, ['name', 'field', 'origin']); $this->_assertImplemented($aggregationDSL, 'geohash_grid', Aggregation\GeohashGrid::class, ['name', 'field']); - $this->_assertImplemented($aggregationDSL, 'geotile_grid', Aggregation\GeotileGrid::class, ['name', 'field']); + $this->_assertImplemented($aggregationDSL, 'geotile_grid', Aggregation\GeotileGridAggregation::class, ['name', 'field']); $this->_assertImplemented($aggregationDSL, 'global', Aggregation\GlobalAggregation::class, ['name']); $this->_assertImplemented($aggregationDSL, 'histogram', Aggregation\Histogram::class, ['name', 'field', 1]); $this->_assertImplemented($aggregationDSL, 'ipv4_range', Aggregation\IpRange::class, ['name', 'field']);