1<?php
2
3namespace Elastica\QueryBuilder\DSL;
4
5use Elastica\Aggregation\AdjacencyMatrix;
6use Elastica\Aggregation\Avg;
7use Elastica\Aggregation\AvgBucket;
8use Elastica\Aggregation\BucketScript;
9use Elastica\Aggregation\Cardinality;
10use Elastica\Aggregation\Composite;
11use Elastica\Aggregation\CumulativeSum;
12use Elastica\Aggregation\DateHistogram;
13use Elastica\Aggregation\DateRange;
14use Elastica\Aggregation\Derivative;
15use Elastica\Aggregation\DiversifiedSampler;
16use Elastica\Aggregation\ExtendedStats;
17use Elastica\Aggregation\Filter;
18use Elastica\Aggregation\Filters;
19use Elastica\Aggregation\GeoDistance;
20use Elastica\Aggregation\GeohashGrid;
21use Elastica\Aggregation\GeotileGridAggregation;
22use Elastica\Aggregation\GlobalAggregation;
23use Elastica\Aggregation\Histogram;
24use Elastica\Aggregation\IpRange;
25use Elastica\Aggregation\Max;
26use Elastica\Aggregation\Min;
27use Elastica\Aggregation\Missing;
28use Elastica\Aggregation\Nested;
29use Elastica\Aggregation\NormalizeAggregation;
30use Elastica\Aggregation\Percentiles;
31use Elastica\Aggregation\PercentilesBucket;
32use Elastica\Aggregation\Range;
33use Elastica\Aggregation\ReverseNested;
34use Elastica\Aggregation\Sampler;
35use Elastica\Aggregation\ScriptedMetric;
36use Elastica\Aggregation\SerialDiff;
37use Elastica\Aggregation\SignificantTerms;
38use Elastica\Aggregation\Stats;
39use Elastica\Aggregation\StatsBucket;
40use Elastica\Aggregation\Sum;
41use Elastica\Aggregation\SumBucket;
42use Elastica\Aggregation\Terms;
43use Elastica\Aggregation\TopHits;
44use Elastica\Aggregation\ValueCount;
45use Elastica\Aggregation\WeightedAvg;
46use Elastica\Exception\NotImplementedException;
47use Elastica\Query\AbstractQuery;
48use Elastica\QueryBuilder\DSL;
49
50/**
51 * Elasticsearch aggregation DSL.
52 *
53 * @author Manuel Andreo Garcia <andreo.garcia@googlemail.com>
54 *
55 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html
56 */
57class Aggregation implements DSL
58{
59    /**
60     * must return type for QueryBuilder usage.
61     */
62    public function getType(): string
63    {
64        return DSL::TYPE_AGGREGATION;
65    }
66
67    /**
68     * min aggregation.
69     *
70     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-min-aggregation.html
71     */
72    public function min(string $name): Min
73    {
74        return new Min($name);
75    }
76
77    /**
78     * max aggregation.
79     *
80     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-max-aggregation.html
81     */
82    public function max(string $name): Max
83    {
84        return new Max($name);
85    }
86
87    /**
88     * sum aggregation.
89     *
90     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-sum-aggregation.html
91     */
92    public function sum(string $name): Sum
93    {
94        return new Sum($name);
95    }
96
97    /**
98     * sum bucket aggregation.
99     *
100     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-sum-bucket-aggregation.html
101     */
102    public function sum_bucket(string $name, ?string $bucketsPath = null): SumBucket
103    {
104        return new SumBucket($name, $bucketsPath);
105    }
106
107    /**
108     * avg aggregation.
109     *
110     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-avg-aggregation.html
111     */
112    public function avg(string $name): Avg
113    {
114        return new Avg($name);
115    }
116
117    /**
118     * avg bucket aggregation.
119     *
120     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-avg-bucket-aggregation.html
121     */
122    public function avg_bucket(string $name, ?string $bucketsPath = null): AvgBucket
123    {
124        return new AvgBucket($name, $bucketsPath);
125    }
126
127    /**
128     * stats aggregation.
129     *
130     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-stats-aggregation.html
131     */
132    public function stats(string $name): Stats
133    {
134        return new Stats($name);
135    }
136
137    /**
138     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-stats-bucket-aggregation.html
139     */
140    public function stats_bucket(string $name, ?string $bucketsPath = null): StatsBucket
141    {
142        return new StatsBucket($name, $bucketsPath);
143    }
144
145    /**
146     * extended stats aggregation.
147     *
148     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-extendedstats-aggregation.html
149     */
150    public function extended_stats(string $name): ExtendedStats
151    {
152        return new ExtendedStats($name);
153    }
154
155    /**
156     * value count aggregation.
157     *
158     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html
159     */
160    public function value_count(string $name, string $field): ValueCount
161    {
162        return new ValueCount($name, $field);
163    }
164
165    /**
166     * percentiles aggregation.
167     *
168     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-percentile-aggregation.html
169     *
170     * @param string      $name  the name of this aggregation
171     * @param string|null $field the field on which to perform this aggregation
172     */
173    public function percentiles(string $name, ?string $field = null): Percentiles
174    {
175        return new Percentiles($name, $field);
176    }
177
178    /**
179     * percentiles_bucket aggregation.
180     *
181     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-percentiles-bucket-aggregation.html
182     *
183     * @param string      $name        the name of this aggregation
184     * @param string|null $bucketsPath the field on which to perform this aggregation
185     */
186    public function percentiles_bucket(string $name, ?string $bucketsPath = null): PercentilesBucket
187    {
188        return new PercentilesBucket($name, $bucketsPath);
189    }
190
191    /**
192     * cardinality aggregation.
193     *
194     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html
195     */
196    public function cardinality(string $name): Cardinality
197    {
198        return new Cardinality($name);
199    }
200
201    /**
202     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-cumulative-sum-aggregation.html
203     */
204    public function cumulative_sum(string $name, string $bucketsPath): CumulativeSum
205    {
206        return new CumulativeSum($name, $bucketsPath);
207    }
208
209    /**
210     * geo bounds aggregation.
211     *
212     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-geobounds-aggregation.html
213     *
214     * @param string $name
215     */
216    public function geo_bounds($name): void
217    {
218        throw new NotImplementedException();
219    }
220
221    /**
222     * top hits aggregation.
223     *
224     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html
225     */
226    public function top_hits(string $name): TopHits
227    {
228        return new TopHits($name);
229    }
230
231    /**
232     * scripted metric aggregation.
233     *
234     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html
235     */
236    public function scripted_metric(
237        string $name,
238        ?string $initScript = null,
239        ?string $mapScript = null,
240        ?string $combineScript = null,
241        ?string $reduceScript = null
242    ): ScriptedMetric {
243        return new ScriptedMetric($name, $initScript, $mapScript, $combineScript, $reduceScript);
244    }
245
246    /**
247     * global aggregation.
248     *
249     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-global-aggregation.html
250     */
251    public function global(string $name): GlobalAggregation
252    {
253        return new GlobalAggregation($name);
254    }
255
256    /**
257     * @deprecated since version 7.1.0, use the "global()" method instead.
258     */
259    public function global_agg(string $name): GlobalAggregation
260    {
261        \trigger_deprecation('ruflin/elastica', '7.1.0', 'The "%s()" method is deprecated, use "global()" instead. It will be removed in 8.0.', __METHOD__);
262
263        return $this->global($name);
264    }
265
266    /**
267     * filter aggregation.
268     *
269     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html
270     */
271    public function filter(string $name, ?AbstractQuery $filter = null): Filter
272    {
273        return new Filter($name, $filter);
274    }
275
276    /**
277     * filters aggregation.
278     *
279     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filters-aggregation.html
280     */
281    public function filters(string $name): Filters
282    {
283        return new Filters($name);
284    }
285
286    /**
287     * missing aggregation.
288     *
289     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-missing-aggregation.html
290     */
291    public function missing(string $name, string $field): Missing
292    {
293        return new Missing($name, $field);
294    }
295
296    /**
297     * nested aggregation.
298     *
299     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-nested-aggregation.html
300     *
301     * @param string $path the nested path for this aggregation
302     */
303    public function nested(string $name, string $path): Nested
304    {
305        return new Nested($name, $path);
306    }
307
308    /**
309     * reverse nested aggregation.
310     *
311     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-reverse-nested-aggregation.html
312     *
313     * @param string      $name The name of this aggregation
314     * @param string|null $path Optional path to the nested object for this aggregation. Defaults to the root of the main document.
315     */
316    public function reverse_nested(string $name, ?string $path = null): ReverseNested
317    {
318        return new ReverseNested($name, $path);
319    }
320
321    /**
322     * terms aggregation.
323     *
324     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html
325     */
326    public function terms(string $name): Terms
327    {
328        return new Terms($name);
329    }
330
331    /**
332     * significant terms aggregation.
333     *
334     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-significantterms-aggregation.html
335     */
336    public function significant_terms(string $name): SignificantTerms
337    {
338        return new SignificantTerms($name);
339    }
340
341    /**
342     * range aggregation.
343     *
344     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-range-aggregation.html
345     */
346    public function range(string $name): Range
347    {
348        return new Range($name);
349    }
350
351    /**
352     * date range aggregation.
353     *
354     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-daterange-aggregation.html
355     */
356    public function date_range(string $name): DateRange
357    {
358        return new DateRange($name);
359    }
360
361    /**
362     * ipv4 range aggregation.
363     *
364     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-iprange-aggregation.html
365     */
366    public function ipv4_range(string $name, string $field): IpRange
367    {
368        return new IpRange($name, $field);
369    }
370
371    /**
372     * histogram aggregation.
373     *
374     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-histogram-aggregation.html
375     *
376     * @param string $name     the name of this aggregation
377     * @param string $field    the name of the field on which to perform the aggregation
378     * @param int    $interval the interval by which documents will be bucketed
379     */
380    public function histogram(string $name, string $field, $interval): Histogram
381    {
382        return new Histogram($name, $field, $interval);
383    }
384
385    /**
386     * date histogram aggregation.
387     *
388     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html
389     *
390     * @param string     $name     the name of this aggregation
391     * @param string     $field    the name of the field on which to perform the aggregation
392     * @param int|string $interval the interval by which documents will be bucketed
393     */
394    public function date_histogram(string $name, string $field, $interval): DateHistogram
395    {
396        return new DateHistogram($name, $field, $interval);
397    }
398
399    /**
400     * geo distance aggregation.
401     *
402     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-geodistance-aggregation.html
403     *
404     * @param string       $name   the name if this aggregation
405     * @param string       $field  the field on which to perform this aggregation
406     * @param array|string $origin the point from which distances will be calculated
407     */
408    public function geo_distance(string $name, string $field, $origin): GeoDistance
409    {
410        return new GeoDistance($name, $field, $origin);
411    }
412
413    /**
414     * geohash grid aggregation.
415     *
416     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-geohashgrid-aggregation.html
417     *
418     * @param string $name  the name of this aggregation
419     * @param string $field the field on which to perform this aggregation
420     */
421    public function geohash_grid(string $name, string $field): GeohashGrid
422    {
423        return new GeohashGrid($name, $field);
424    }
425
426    /**
427     * geotile grid aggregation.
428     *
429     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-geotilegrid-aggregation.html
430     *
431     * @param string $name  the name of this aggregation
432     * @param string $field the field on which to perform this aggregation
433     */
434    public function geotile_grid(string $name, string $field): GeotileGridAggregation
435    {
436        return new GeotileGridAggregation($name, $field);
437    }
438
439    /**
440     * bucket script aggregation.
441     *
442     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-script-aggregation.html
443     */
444    public function bucket_script(string $name, ?array $bucketsPath = null, ?string $script = null): BucketScript
445    {
446        return new BucketScript($name, $bucketsPath, $script);
447    }
448
449    /**
450     * serial diff aggregation.
451     *
452     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-serialdiff-aggregation.html
453     */
454    public function serial_diff(string $name, ?string $bucketsPath = null): SerialDiff
455    {
456        return new SerialDiff($name, $bucketsPath);
457    }
458
459    /**
460     * adjacency matrix aggregation.
461     *
462     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-adjacency-matrix-aggregation.html
463     */
464    public function adjacency_matrix(string $name): AdjacencyMatrix
465    {
466        return new AdjacencyMatrix($name);
467    }
468
469    /**
470     * sampler aggregation.
471     *
472     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-sampler-aggregation.html
473     */
474    public function sampler(string $name): Sampler
475    {
476        return new Sampler($name);
477    }
478
479    /**
480     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-derivative-aggregation.html
481     */
482    public function derivative(string $name, ?string $bucketsPath = null): Derivative
483    {
484        return new Derivative($name, $bucketsPath);
485    }
486
487    /**
488     * diversified sampler aggregation.
489     *
490     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-diversified-sampler-aggregation.html
491     */
492    public function diversified_sampler(string $name): DiversifiedSampler
493    {
494        return new DiversifiedSampler($name);
495    }
496
497    /**
498     * weighted avg aggregation.
499     *
500     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-weight-avg-aggregation.html
501     */
502    public function weighted_avg(string $name): WeightedAvg
503    {
504        return new WeightedAvg($name);
505    }
506
507    /**
508     * composite aggregation.
509     *
510     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html
511     */
512    public function composite(string $name): Composite
513    {
514        return new Composite($name);
515    }
516
517    /**
518     * normalize aggregation.
519     *
520     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-normalize-aggregation.html
521     */
522    public function normalize(string $name, ?string $bucketsPath = null, ?string $method = null): NormalizeAggregation
523    {
524        return new NormalizeAggregation($name, $bucketsPath, $method);
525    }
526}
527