1<?php
2
3namespace Elastica\Aggregation;
4
5/**
6 * Class Percentiles.
7 *
8 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-percentile-aggregation.html
9 */
10class Percentiles extends AbstractSimpleAggregation
11{
12    use Traits\KeyedTrait;
13    use Traits\MissingTrait;
14
15    /**
16     * @param string      $name  the name of this aggregation
17     * @param string|null $field the field on which to perform this aggregation
18     */
19    public function __construct(string $name, ?string $field = null)
20    {
21        parent::__construct($name);
22
23        if (null !== $field) {
24            $this->setField($field);
25        }
26    }
27
28    /**
29     * Set compression parameter.
30     *
31     * @return $this
32     */
33    public function setCompression(float $value): self
34    {
35        $compression = ['compression' => $value];
36
37        return $this->setParam('tdigest', $compression);
38    }
39
40    /**
41     * Set hdr parameter.
42     *
43     * @return $this
44     */
45    public function setHdr(string $key, float $value): self
46    {
47        $compression = [$key => $value];
48
49        return $this->setParam('hdr', $compression);
50    }
51
52    /**
53     * Set which percents must be returned.
54     *
55     * @param float[] $percents
56     *
57     * @return $this
58     */
59    public function setPercents(array $percents): self
60    {
61        return $this->setParam('percents', $percents);
62    }
63
64    /**
65     * Add yet another percent to result.
66     *
67     * @return $this
68     */
69    public function addPercent(float $percent): self
70    {
71        return $this->addParam('percents', $percent);
72    }
73}
74