|
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
|
|
|
namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; |
|
13
|
|
|
|
|
14
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; |
|
15
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\Type\BucketingTrait; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class representing RangeAggregation. |
|
19
|
|
|
* |
|
20
|
|
|
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-range-aggregation.html |
|
21
|
|
|
*/ |
|
22
|
|
|
class RangeAggregation extends AbstractAggregation |
|
23
|
|
|
{ |
|
24
|
|
|
use BucketingTrait; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
private $ranges = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var bool |
|
33
|
|
|
*/ |
|
34
|
|
|
private $keyed = false; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Inner aggregations container init. |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $name |
|
40
|
|
|
* @param string $field |
|
41
|
|
|
* @param array $ranges |
|
42
|
|
|
* @param bool $keyed |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct($name, $field = null, $ranges = [], $keyed = false) |
|
45
|
|
|
{ |
|
46
|
|
|
parent::__construct($name); |
|
47
|
|
|
|
|
48
|
|
|
$this->setField($field); |
|
49
|
|
|
$this->setKeyed($keyed); |
|
50
|
|
|
foreach ($ranges as $range) { |
|
51
|
|
|
$from = isset($range['from']) ? $range['from'] : null; |
|
52
|
|
|
$to = isset($range['to']) ? $range['to'] : null; |
|
53
|
|
|
$key = isset($range['key']) ? $range['key'] : null; |
|
54
|
|
|
$this->addRange($from, $to, $key); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Sets if result buckets should be keyed. |
|
60
|
|
|
* |
|
61
|
|
|
* @param bool $keyed |
|
62
|
|
|
* |
|
63
|
|
|
* @return RangeAggregation |
|
64
|
|
|
*/ |
|
65
|
|
|
public function setKeyed($keyed) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->keyed = $keyed; |
|
68
|
|
|
|
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Add range to aggregation. |
|
74
|
|
|
* |
|
75
|
|
|
* @param int|float|null $from |
|
76
|
|
|
* @param int|float|null $to |
|
77
|
|
|
* @param string $key |
|
78
|
|
|
* |
|
79
|
|
|
* @return RangeAggregation |
|
80
|
|
|
*/ |
|
81
|
|
|
public function addRange($from = null, $to = null, $key = '') |
|
82
|
|
|
{ |
|
83
|
|
|
$range = array_filter( |
|
84
|
|
|
[ |
|
85
|
|
|
'from' => $from, |
|
86
|
|
|
'to' => $to, |
|
87
|
|
|
] |
|
88
|
|
|
); |
|
89
|
|
|
|
|
90
|
|
|
if ($this->keyed && !empty($key)) { |
|
91
|
|
|
$range['key'] = $key; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$this->ranges[] = $range; |
|
95
|
|
|
|
|
96
|
|
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Remove range from aggregation. Returns true on success. |
|
101
|
|
|
* |
|
102
|
|
|
* @param int|float|null $from |
|
103
|
|
|
* @param int|float|null $to |
|
104
|
|
|
* |
|
105
|
|
|
* @return bool |
|
106
|
|
|
*/ |
|
107
|
|
|
public function removeRange($from, $to) |
|
108
|
|
|
{ |
|
109
|
|
|
foreach ($this->ranges as $key => $range) { |
|
110
|
|
|
if (array_diff_assoc(array_filter(['from' => $from, 'to' => $to]), $range) === []) { |
|
111
|
|
|
unset($this->ranges[$key]); |
|
112
|
|
|
|
|
113
|
|
|
return true; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return false; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Removes range by key. |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $key Range key. |
|
124
|
|
|
* |
|
125
|
|
|
* @return bool |
|
126
|
|
|
*/ |
|
127
|
|
|
public function removeRangeByKey($key) |
|
128
|
|
|
{ |
|
129
|
|
|
if ($this->keyed) { |
|
130
|
|
|
foreach ($this->ranges as $rangeKey => $range) { |
|
131
|
|
|
if (array_key_exists('key', $range) && $range['key'] === $key) { |
|
132
|
|
|
unset($this->ranges[$rangeKey]); |
|
133
|
|
|
|
|
134
|
|
|
return true; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return false; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* {@inheritdoc} |
|
144
|
|
|
*/ |
|
145
|
|
|
public function getArray() |
|
146
|
|
|
{ |
|
147
|
|
|
$data = [ |
|
148
|
|
|
'keyed' => $this->keyed, |
|
149
|
|
|
'ranges' => array_values($this->ranges), |
|
150
|
|
|
]; |
|
151
|
|
|
|
|
152
|
|
|
if ($this->getField()) { |
|
153
|
|
|
$data['field'] = $this->getField(); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
return $data; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* {@inheritdoc} |
|
161
|
|
|
*/ |
|
162
|
|
|
public function getType() |
|
163
|
|
|
{ |
|
164
|
|
|
return 'range'; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|