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