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 geotile grid support #1880

Merged
merged 4 commits into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -29,7 +29,9 @@ 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\GeotileGridAggregation` [#1880](https://github.com/ruflin/Elastica/pull/1880)
* Added `Elastica\Aggregation\Avg::setMissing()`, `Elastica\Aggregation\Cardinality::setMissing()`, `Elastica\Aggregation\DateRange::setMissing()`, `Elastica\Aggregation\DateHistogram::setMissing()`, `Elastica\Aggregation\ExtendedStats::setMissing()`, `Elastica\Aggregation\Histogram::setMissing()`, `Elastica\Aggregation\Max::setMissing()`, `Elastica\Aggregation\Min::setMissing()`, `Elastica\Aggregation\Stats::setMissing()`, `Elastica\Aggregation\Sum::setMissing()`, `Elastica\Aggregation\Terms::setMissing()` [#1876](https://github.com/ruflin/Elastica/pull/1876)

### 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)
Expand Down
62 changes: 62 additions & 0 deletions src/Aggregation/GeotileGridAggregation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Elastica\Aggregation;

/**
* Class GeotileGridAggregation.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-geotilegrid-aggregation.html
*/
class GeotileGridAggregation extends AbstractAggregation
{
use Traits\ShardSizeTrait;

public const DEFAULT_PRECISION_VALUE = 7;
public const DEFAULT_SIZE_VALUE = 10000;

/**
* @param string $name the name of this aggregation
* @param string $field the field on which to perform this aggregation
*/
public function __construct(string $name, string $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(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);
}
}
14 changes: 14 additions & 0 deletions src/QueryBuilder/DSL/Aggregation.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Elastica\Aggregation\Filters;
use Elastica\Aggregation\GeoDistance;
use Elastica\Aggregation\GeohashGrid;
use Elastica\Aggregation\GeotileGridAggregation;
use Elastica\Aggregation\GlobalAggregation;
use Elastica\Aggregation\Histogram;
use Elastica\Aggregation\IpRange;
Expand Down Expand Up @@ -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): GeotileGridAggregation
{
return new GeotileGridAggregation($name, $field);
}

/**
* bucket script aggregation.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/Aggregation/GeoCentroidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class GeoCentroidTest extends BaseAggregationTest
/**
* @group functional
*/
public function testGeohashGridAggregation(): void
public function testGeoCentroidGridAggregation(): void
{
$agg = new GeoCentroid('centroid', 'location');

Expand Down
49 changes: 49 additions & 0 deletions tests/Aggregation/GeotileGridAggregationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Elastica\Test\Aggregation;

use Elastica\Aggregation\GeotileGridAggregation;
use Elastica\Document;
use Elastica\Index;
use Elastica\Mapping;
use Elastica\Query;

/**
* @internal
*/
class GeotileGridAggregationTest extends BaseAggregationTest
{
/**
* @group functional
*/
public function testGeotileGridAggregation(): void
{
$agg = new GeotileGridAggregation('tile', 'location');
$agg->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;
}
}
1 change: 1 addition & 0 deletions tests/QueryBuilder/DSL/AggregationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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\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']);
Expand Down