-
Notifications
You must be signed in to change notification settings - Fork 736
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added geotile grid support * added geotile grid support to CHANGELOG.md * class renamed + use of new trait Co-authored-by: Nicolas Ruflin <[email protected]>
- Loading branch information
Showing
6 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters