1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elastica\Test\Aggregation; |
4
|
|
|
|
5
|
|
|
use Elastica\Aggregation\GeoDistance; |
6
|
|
|
use Elastica\Document; |
7
|
|
|
use Elastica\Index; |
8
|
|
|
use Elastica\Mapping; |
9
|
|
|
use Elastica\Query; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @internal |
13
|
|
|
*/ |
14
|
|
|
class GeoDistanceTest extends BaseAggregationTest |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @group functional |
18
|
|
|
*/ |
19
|
|
View Code Duplication |
public function testGeoDistanceAggregation(): void |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
$agg = new GeoDistance('geo', 'location', ['lat' => 32.804654, 'lon' => -117.242594]); |
22
|
|
|
$agg->addRange(null, 100); |
23
|
|
|
$agg->setUnit('mi'); |
24
|
|
|
|
25
|
|
|
$query = new Query(); |
26
|
|
|
$query->addAggregation($agg); |
27
|
|
|
$results = $this->_getIndexForTest()->search($query)->getAggregation('geo'); |
28
|
|
|
|
29
|
|
|
$this->assertEquals(2, $results['buckets'][0]['doc_count']); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @group functional |
34
|
|
|
*/ |
35
|
|
|
public function testGeoDistanceKeyedAggregation(): void |
36
|
|
|
{ |
37
|
|
|
$agg = new GeoDistance('geo', 'location', ['lat' => 32.804654, 'lon' => -117.242594]); |
38
|
|
|
$agg->addRange(null, 100); |
39
|
|
|
$agg->setKeyed(); |
40
|
|
|
$agg->setUnit('mi'); |
41
|
|
|
|
42
|
|
|
$query = new Query(); |
43
|
|
|
$query->addAggregation($agg); |
44
|
|
|
$results = $this->_getIndexForTest()->search($query)->getAggregation('geo'); |
45
|
|
|
|
46
|
|
|
$expected = [ |
47
|
|
|
'*-100.0', |
48
|
|
|
]; |
49
|
|
|
$this->assertSame($expected, \array_keys($results['buckets'])); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
View Code Duplication |
protected function _getIndexForTest(): Index |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$index = $this->_createIndex(); |
55
|
|
|
$index->setMapping(new Mapping([ |
56
|
|
|
'location' => ['type' => 'geo_point'], |
57
|
|
|
])); |
58
|
|
|
|
59
|
|
|
$index->addDocuments([ |
60
|
|
|
new Document(1, ['location' => ['lat' => 32.849437, 'lon' => -117.271732]]), |
61
|
|
|
new Document(2, ['location' => ['lat' => 32.798320, 'lon' => -117.246648]]), |
62
|
|
|
new Document(3, ['location' => ['lat' => 37.782439, 'lon' => -122.392560]]), |
63
|
|
|
]); |
64
|
|
|
|
65
|
|
|
$index->refresh(); |
66
|
|
|
|
67
|
|
|
return $index; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.