1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; |
15
|
|
|
|
16
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; |
17
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\Type\BucketingTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class representing geo distance aggregation. |
21
|
|
|
* |
22
|
|
|
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-geodistance-aggregation.html |
23
|
|
|
*/ |
24
|
|
|
class GeoDistanceAggregation extends AbstractAggregation |
25
|
|
|
{ |
26
|
|
|
use BucketingTrait; |
27
|
|
|
|
28
|
|
|
private mixed $origin; |
|
|
|
|
29
|
|
|
|
30
|
|
|
private array $ranges = []; |
31
|
|
|
|
32
|
|
|
public function __construct( |
33
|
|
|
private string $name, |
34
|
|
|
?string $field = null, |
35
|
|
|
mixed $origin = null, |
36
|
|
|
array $ranges = [], |
37
|
|
|
?string $unit = null, |
38
|
|
|
?string $distanceType = null |
39
|
|
|
) { |
40
|
|
|
parent::__construct($name); |
41
|
|
|
|
42
|
|
|
$this->setField($field); |
43
|
|
|
$this->setOrigin($origin); |
44
|
|
|
foreach ($ranges as $range) { |
45
|
|
|
$from = isset($range['from']) ? $range['from'] : null; |
46
|
|
|
$to = isset($range['to']) ? $range['to'] : null; |
47
|
|
|
$this->addRange($from, $to); |
48
|
|
|
} |
49
|
|
|
$this->setUnit($unit); |
50
|
|
|
$this->setDistanceType($distanceType); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getOrigin(): mixed |
54
|
|
|
{ |
55
|
|
|
return $this->origin; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function setOrigin(mixed $origin): static |
59
|
|
|
{ |
60
|
|
|
$this->origin = $origin; |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getDistanceType(): ?string |
66
|
|
|
{ |
67
|
|
|
return $this->distanceType; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function setDistanceType(?string $distanceType): static |
71
|
|
|
{ |
72
|
|
|
$this->distanceType = $distanceType; |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getUnit(): ?string |
78
|
|
|
{ |
79
|
|
|
return $this->unit; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function setUnit(?string $unit): static |
83
|
|
|
{ |
84
|
|
|
$this->unit = $unit; |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Add range to aggregation. |
91
|
|
|
*/ |
92
|
|
|
public function addRange(null|int|float|string $from = null, null|int|float|string $to = null): static |
93
|
|
|
{ |
94
|
|
|
$range = array_filter( |
95
|
|
|
[ |
96
|
|
|
'from' => $from, |
97
|
|
|
'to' => $to, |
98
|
|
|
] |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
if (empty($range)) { |
102
|
|
|
throw new \LogicException('Either from or to must be set. Both cannot be null.'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->ranges[] = $range; |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function getArray(): array |
111
|
|
|
{ |
112
|
|
|
$data = []; |
113
|
|
|
|
114
|
|
|
if ($this->getField()) { |
115
|
|
|
$data['field'] = $this->getField(); |
116
|
|
|
} else { |
117
|
|
|
throw new \LogicException('Geo distance aggregation must have a field set.'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if ($this->getOrigin()) { |
121
|
|
|
$data['origin'] = $this->getOrigin(); |
122
|
|
|
} else { |
123
|
|
|
throw new \LogicException('Geo distance aggregation must have an origin set.'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if ($this->getUnit()) { |
127
|
|
|
$data['unit'] = $this->getUnit(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if ($this->getDistanceType()) { |
131
|
|
|
$data['distance_type'] = $this->getDistanceType(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$data['ranges'] = $this->ranges; |
135
|
|
|
|
136
|
|
|
return $data; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function getType(): string |
140
|
|
|
{ |
141
|
|
|
return 'geo_distance'; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|