Skip to content

Commit c51b030

Browse files
cobak78ruflin
authored andcommitted
add geo_bound aggregation (#1271)
1 parent 0e81d47 commit c51b030

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ All notable changes to this project will be documented in this file based on the
4646
- added `\Elastica\Query\AbstractGeoShape::RELATION_WITHIN`
4747
- Date math in index names is now escaped in URI
4848
- Added a check for paths that already have date math escaped
49+
- added `\Elastica\Aggregation\GeoBounds` to retrieve viewport with geo_points on it
4950

5051
### Improvements
5152

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
namespace Elastica\Aggregation;
3+
4+
/**
5+
* Class GeoBounds.
6+
*
7+
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-geobounds-aggregation.html
8+
*/
9+
class GeoBounds extends AbstractAggregation
10+
{
11+
/**
12+
* @param string $name the name of this aggregation
13+
* @param string $field the field on which to perform this aggregation
14+
*/
15+
public function __construct($name, $field)
16+
{
17+
parent::__construct($name);
18+
$this->setField($field);
19+
}
20+
21+
/**
22+
* Set the field for this aggregation.
23+
*
24+
* @param string $field the name of the document field on which to perform this aggregation
25+
*
26+
* @return $this
27+
*/
28+
public function setField($field)
29+
{
30+
return $this->setParam('field', $field);
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
namespace Elastica\Test\Aggregation;
3+
4+
use Elastica\Aggregation\GeoBounds;
5+
use Elastica\Document;
6+
use Elastica\Query;
7+
use Elastica\Type\Mapping;
8+
9+
class GeoBoundsTest extends BaseAggregationTest
10+
{
11+
protected function _getIndexForTest()
12+
{
13+
$index = $this->_createIndex();
14+
$type = $index->getType('test');
15+
16+
$type->setMapping(new Mapping(null, [
17+
'location' => ['type' => 'geo_point'],
18+
]));
19+
20+
$type->addDocuments([
21+
new Document(1, ['location' => ['lat' => 32.849437, 'lon' => -117.271732]]),
22+
new Document(2, ['location' => ['lat' => 32.798320, 'lon' => -117.246648]]),
23+
new Document(3, ['location' => ['lat' => 37.782439, 'lon' => -122.392560]]),
24+
]);
25+
26+
$index->refresh();
27+
28+
return $index;
29+
}
30+
31+
/**
32+
* @group functional
33+
*/
34+
public function testGeoBoundsAggregation()
35+
{
36+
$agg = new GeoBounds('viewport', 'location');
37+
38+
$query = new Query();
39+
$query->addAggregation($agg);
40+
$results = $this->_getIndexForTest()->search($query)->getAggregation('viewport');
41+
42+
$this->assertEquals(37.782438984141, $results['bounds']['top_left']['lat']);
43+
$this->assertEquals(-122.39256000146, $results['bounds']['top_left']['lon']);
44+
$this->assertEquals(32.798319971189, $results['bounds']['bottom_right']['lat']);
45+
$this->assertEquals(-117.24664804526, $results['bounds']['bottom_right']['lon']);
46+
}
47+
}

0 commit comments

Comments
 (0)