|
1
|
|
|
<?php |
|
2
|
|
|
namespace Triadev\Leopard\Business\Dsl\Aggregation; |
|
3
|
|
|
|
|
4
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\ChildrenAggregation; |
|
5
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\DateHistogramAggregation; |
|
6
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\DateRangeAggregation; |
|
7
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\DiversifiedSamplerAggregation; |
|
8
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\FilterAggregation; |
|
9
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\FiltersAggregation; |
|
10
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\GeoDistanceAggregation; |
|
11
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\GeoHashGridAggregation; |
|
12
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\HistogramAggregation; |
|
13
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\Ipv4RangeAggregation; |
|
14
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\MissingAggregation; |
|
15
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\NestedAggregation; |
|
16
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\RangeAggregation; |
|
17
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\SamplerAggregation; |
|
18
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\SignificantTermsAggregation; |
|
19
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\TermsAggregation; |
|
20
|
|
|
use ONGR\ElasticsearchDSL\BuilderInterface; |
|
21
|
|
|
|
|
22
|
|
|
class Bucketing extends Aggs |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Children |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $name |
|
28
|
|
|
* @param string $children |
|
29
|
|
|
* @param \Closure $bucketing |
|
30
|
|
|
* @return Bucketing |
|
31
|
|
|
*/ |
|
32
|
1 |
|
public function children(string $name, string $children, \Closure $bucketing) : Bucketing |
|
33
|
|
|
{ |
|
34
|
1 |
|
$resultAgg = new ChildrenAggregation($name, $children); |
|
35
|
|
|
|
|
36
|
1 |
|
$bucketingBuilder = new self(); |
|
37
|
1 |
|
$bucketing($bucketingBuilder); |
|
38
|
|
|
|
|
39
|
1 |
|
foreach ($bucketingBuilder->getAggregations() as $agg) { |
|
40
|
1 |
|
$resultAgg->addAggregation($agg); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
$this->addAggregation($resultAgg); |
|
44
|
1 |
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Date histogram |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $name |
|
51
|
|
|
* @param string $field |
|
52
|
|
|
* @param string $interval |
|
53
|
|
|
* @param string|null $format |
|
54
|
|
|
* @return Bucketing |
|
55
|
|
|
* |
|
56
|
|
|
* @throws \InvalidArgumentException |
|
57
|
|
|
*/ |
|
58
|
1 |
|
public function dateHistogram( |
|
59
|
|
|
string $name, |
|
60
|
|
|
string $field, |
|
61
|
|
|
string $interval, |
|
62
|
|
|
?string $format = null |
|
63
|
|
|
) : Bucketing { |
|
64
|
|
|
$validInterval = [ |
|
65
|
1 |
|
'year', |
|
66
|
|
|
'quarter', |
|
67
|
|
|
'month', |
|
68
|
|
|
'week', |
|
69
|
|
|
'day', |
|
70
|
|
|
'hour', |
|
71
|
|
|
'minute', |
|
72
|
|
|
'second', |
|
73
|
|
|
]; |
|
74
|
|
|
|
|
75
|
1 |
|
if (!in_array($interval, $validInterval)) { |
|
76
|
|
|
throw new \InvalidArgumentException(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
$this->addAggregation(new DateHistogramAggregation($name, $field, $interval, $format)); |
|
80
|
1 |
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Date range |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $name |
|
87
|
|
|
* @param string $field |
|
88
|
|
|
* @param string $format |
|
89
|
|
|
* @param array $ranges |
|
90
|
|
|
* @return Bucketing |
|
91
|
|
|
*/ |
|
92
|
1 |
|
public function dateRange( |
|
93
|
|
|
string $name, |
|
94
|
|
|
string $field, |
|
95
|
|
|
string $format, |
|
96
|
|
|
array $ranges = [] |
|
97
|
|
|
) : Bucketing { |
|
98
|
1 |
|
$this->addAggregation(new DateRangeAggregation($name, $field, $format, $ranges)); |
|
99
|
1 |
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Diversified sampler |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $name |
|
106
|
|
|
* @param string $field |
|
107
|
|
|
* @param int|null $shardSize |
|
108
|
|
|
* @return Bucketing |
|
109
|
|
|
*/ |
|
110
|
1 |
|
public function diversifiedSampler(string $name, string $field, ?int $shardSize = null) : Bucketing |
|
111
|
|
|
{ |
|
112
|
1 |
|
$this->addAggregation(new DiversifiedSamplerAggregation($name, $field, $shardSize)); |
|
113
|
1 |
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Filter |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $name |
|
120
|
|
|
* @param BuilderInterface $aggregation |
|
121
|
|
|
* @param BuilderInterface[] $aggregations |
|
122
|
|
|
* @return Bucketing |
|
123
|
|
|
*/ |
|
124
|
1 |
|
public function filter(string $name, BuilderInterface $aggregation, array $aggregations) : Bucketing |
|
125
|
|
|
{ |
|
126
|
1 |
|
$agg = new FilterAggregation($name, $aggregation); |
|
127
|
|
|
|
|
128
|
1 |
|
foreach ($aggregations as $a) { |
|
129
|
1 |
|
if ($a instanceof BuilderInterface) { |
|
130
|
1 |
|
$agg->addAggregation($a); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
1 |
|
$this->addAggregation($agg); |
|
135
|
1 |
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Filters |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $name |
|
142
|
|
|
* @param BuilderInterface[] $filters |
|
143
|
|
|
* @param BuilderInterface[] $aggregations |
|
144
|
|
|
* @param bool $anonymous |
|
145
|
|
|
* @return Bucketing |
|
146
|
|
|
*/ |
|
147
|
1 |
|
public function filters(string $name, array $filters, array $aggregations, bool $anonymous = false) : Bucketing |
|
148
|
|
|
{ |
|
149
|
1 |
|
$agg = new FiltersAggregation($name, $filters, $anonymous); |
|
150
|
|
|
|
|
151
|
1 |
|
foreach ($aggregations as $a) { |
|
152
|
1 |
|
if ($a instanceof BuilderInterface) { |
|
153
|
1 |
|
$agg->addAggregation($a); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
1 |
|
$this->addAggregation($agg); |
|
158
|
|
|
|
|
159
|
1 |
|
return $this; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Geo distance |
|
164
|
|
|
* |
|
165
|
|
|
* @param string $name |
|
166
|
|
|
* @param string $field |
|
167
|
|
|
* @param string $origin |
|
168
|
|
|
* @param array $ranges |
|
169
|
|
|
* @param string|null $unit |
|
170
|
|
|
* @param string|null $distanceType |
|
171
|
|
|
* @return Bucketing |
|
172
|
|
|
*/ |
|
173
|
1 |
|
public function geoDistance( |
|
174
|
|
|
string $name, |
|
175
|
|
|
string $field, |
|
176
|
|
|
string $origin, |
|
177
|
|
|
array $ranges = [], |
|
178
|
|
|
string $unit = null, |
|
179
|
|
|
string $distanceType = null |
|
180
|
|
|
) : Bucketing { |
|
181
|
1 |
|
$this->addAggregation( |
|
182
|
1 |
|
new GeoDistanceAggregation( |
|
183
|
1 |
|
$name, |
|
184
|
1 |
|
$field, |
|
185
|
1 |
|
$origin, |
|
186
|
1 |
|
$ranges, |
|
187
|
1 |
|
$unit, |
|
188
|
1 |
|
$distanceType |
|
189
|
|
|
) |
|
190
|
|
|
); |
|
191
|
|
|
|
|
192
|
1 |
|
return $this; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Geo hash grid |
|
197
|
|
|
* |
|
198
|
|
|
* @param string $name |
|
199
|
|
|
* @param string $field |
|
200
|
|
|
* @param int|null $precision |
|
201
|
|
|
* @param int|null $size |
|
202
|
|
|
* @param int|null $shardSize |
|
203
|
|
|
* @return Bucketing |
|
204
|
|
|
*/ |
|
205
|
1 |
|
public function geoHashGrid( |
|
206
|
|
|
string $name, |
|
207
|
|
|
string $field, |
|
208
|
|
|
?int $precision = null, |
|
209
|
|
|
?int $size = null, |
|
210
|
|
|
?int $shardSize = null |
|
211
|
|
|
) : Bucketing { |
|
212
|
1 |
|
$this->addAggregation( |
|
213
|
1 |
|
new GeoHashGridAggregation( |
|
214
|
1 |
|
$name, |
|
215
|
1 |
|
$field, |
|
216
|
1 |
|
$precision, |
|
217
|
1 |
|
$size, |
|
218
|
1 |
|
$shardSize |
|
219
|
|
|
) |
|
220
|
|
|
); |
|
221
|
|
|
|
|
222
|
1 |
|
return $this; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Histogram |
|
227
|
|
|
* |
|
228
|
|
|
* @param string $name |
|
229
|
|
|
* @param string $field |
|
230
|
|
|
* @param int $interval |
|
231
|
|
|
* @param int|null $minDocCount |
|
232
|
|
|
* @param string|null $orderMode |
|
233
|
|
|
* @param string $orderDirection |
|
234
|
|
|
* @param int|null $extendedBoundsMin |
|
235
|
|
|
* @param int|null $extendedBoundsMax |
|
236
|
|
|
* @param bool|null $keyed |
|
237
|
|
|
* @return Bucketing |
|
238
|
|
|
*/ |
|
239
|
1 |
|
public function histogram( |
|
240
|
|
|
string $name, |
|
241
|
|
|
string $field, |
|
242
|
|
|
int $interval, |
|
243
|
|
|
?int $minDocCount = null, |
|
244
|
|
|
?string $orderMode = null, |
|
245
|
|
|
string $orderDirection = HistogramAggregation::DIRECTION_ASC, |
|
246
|
|
|
?int $extendedBoundsMin = null, |
|
247
|
|
|
?int $extendedBoundsMax = null, |
|
248
|
|
|
bool $keyed = null |
|
249
|
|
|
) : Bucketing { |
|
250
|
1 |
|
$this->addAggregation( |
|
251
|
1 |
|
new HistogramAggregation( |
|
252
|
1 |
|
$name, |
|
253
|
1 |
|
$field, |
|
254
|
1 |
|
$interval, |
|
255
|
1 |
|
$minDocCount, |
|
256
|
1 |
|
$orderMode, |
|
257
|
1 |
|
$orderDirection, |
|
258
|
1 |
|
$extendedBoundsMin, |
|
259
|
1 |
|
$extendedBoundsMax, |
|
260
|
1 |
|
$keyed |
|
261
|
|
|
) |
|
262
|
|
|
); |
|
263
|
|
|
|
|
264
|
1 |
|
return $this; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* Ipv4 |
|
269
|
|
|
* |
|
270
|
|
|
* @param string $name |
|
271
|
|
|
* @param string $field |
|
272
|
|
|
* @param array $ranges |
|
273
|
|
|
* @return Bucketing |
|
274
|
|
|
*/ |
|
275
|
1 |
|
public function ipv4Range(string $name, string $field, array $ranges = []) : Bucketing |
|
276
|
|
|
{ |
|
277
|
1 |
|
$this->addAggregation( |
|
278
|
1 |
|
new Ipv4RangeAggregation( |
|
279
|
1 |
|
$name, |
|
280
|
1 |
|
$field, |
|
281
|
1 |
|
$ranges |
|
282
|
|
|
) |
|
283
|
|
|
); |
|
284
|
|
|
|
|
285
|
1 |
|
return $this; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* Missing |
|
290
|
|
|
* |
|
291
|
|
|
* @param string $name |
|
292
|
|
|
* @param string $field |
|
293
|
|
|
* @return Bucketing |
|
294
|
|
|
*/ |
|
295
|
1 |
|
public function missing(string $name, string $field) : Bucketing |
|
296
|
|
|
{ |
|
297
|
1 |
|
$this->addAggregation(new MissingAggregation($name, $field)); |
|
298
|
1 |
|
return $this; |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* Nested |
|
303
|
|
|
* |
|
304
|
|
|
* @param string $name |
|
305
|
|
|
* @param string $path |
|
306
|
|
|
* @param array $aggregations |
|
307
|
|
|
* @return Bucketing |
|
308
|
|
|
*/ |
|
309
|
1 |
|
public function nested(string $name, string $path, array $aggregations = []) : Bucketing |
|
310
|
|
|
{ |
|
311
|
1 |
|
$agg = new NestedAggregation($name, $path); |
|
312
|
|
|
|
|
313
|
1 |
|
foreach ($aggregations as $aggregation) { |
|
314
|
1 |
|
if ($aggregation instanceof BuilderInterface) { |
|
315
|
1 |
|
$agg->addAggregation($aggregation); |
|
316
|
|
|
} |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
1 |
|
$this->addAggregation($agg); |
|
320
|
1 |
|
return $this; |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* Range |
|
325
|
|
|
* |
|
326
|
|
|
* @param string $name |
|
327
|
|
|
* @param string $field |
|
328
|
|
|
* @param array $ranges |
|
329
|
|
|
* @param bool $keyed |
|
330
|
|
|
* @return Bucketing |
|
331
|
|
|
*/ |
|
332
|
1 |
|
public function range(string $name, string $field, array $ranges = [], bool $keyed = false) : Bucketing |
|
333
|
|
|
{ |
|
334
|
1 |
|
$this->addAggregation(new RangeAggregation($name, $field, $ranges, $keyed)); |
|
335
|
1 |
|
return $this; |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
public function reverseNested() : Bucketing |
|
339
|
|
|
{ |
|
340
|
|
|
return $this; |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
/** |
|
344
|
|
|
* Sampler |
|
345
|
|
|
* |
|
346
|
|
|
* @param string $name |
|
347
|
|
|
* @param string $field |
|
348
|
|
|
* @param int|null $shardSize |
|
349
|
|
|
* @param array $aggregations |
|
350
|
|
|
* @return Bucketing |
|
351
|
|
|
*/ |
|
352
|
1 |
|
public function sampler(string $name, string $field, ?int $shardSize = null, array $aggregations = []) : Bucketing |
|
353
|
|
|
{ |
|
354
|
1 |
|
$agg = new SamplerAggregation($name, $field, $shardSize); |
|
355
|
|
|
|
|
356
|
1 |
|
foreach ($aggregations as $aggregation) { |
|
357
|
1 |
|
if ($aggregation instanceof BuilderInterface) { |
|
358
|
1 |
|
$agg->addAggregation($aggregation); |
|
359
|
|
|
} |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
1 |
|
$this->addAggregation($agg); |
|
363
|
1 |
|
return $this; |
|
364
|
|
|
} |
|
365
|
|
|
|
|
366
|
|
|
/** |
|
367
|
|
|
* Significant terms |
|
368
|
|
|
* |
|
369
|
|
|
* @param string $name |
|
370
|
|
|
* @param string $field |
|
371
|
|
|
* @param string|null $script |
|
372
|
|
|
* @return Bucketing |
|
373
|
|
|
*/ |
|
374
|
1 |
|
public function significantTerms(string $name, string $field, ?string $script = null) : Bucketing |
|
375
|
|
|
{ |
|
376
|
1 |
|
$this->addAggregation(new SignificantTermsAggregation($name, $field, $script)); |
|
377
|
1 |
|
return $this; |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
|
|
/** |
|
381
|
|
|
* Terms |
|
382
|
|
|
* |
|
383
|
|
|
* @param string $name |
|
384
|
|
|
* @param string|null $field |
|
385
|
|
|
* @param string|null $script |
|
386
|
|
|
* @return Bucketing |
|
387
|
|
|
*/ |
|
388
|
2 |
|
public function terms(string $name, ?string $field = null, ?string $script = null) : Bucketing |
|
389
|
|
|
{ |
|
390
|
2 |
|
$this->addAggregation(new TermsAggregation($name, $field, $script)); |
|
391
|
2 |
|
return $this; |
|
392
|
|
|
} |
|
393
|
|
|
} |
|
394
|
|
|
|