1<?php
2
3namespace Elastica\Aggregation;
4
5/**
6 * Class Histogram.
7 *
8 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-histogram-aggregation.html
9 */
10class Histogram 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     $field    the name of the field on which to perform the aggregation
18     * @param int|string $interval the interval by which documents will be bucketed
19     */
20    public function __construct(string $name, string $field, $interval)
21    {
22        parent::__construct($name);
23        $this->setField($field);
24        $this->setInterval($interval);
25    }
26
27    /**
28     * Set the interval by which documents will be bucketed.
29     *
30     * @param int|string $interval
31     *
32     * @return $this
33     */
34    public function setInterval($interval)
35    {
36        return $this->setParam('interval', $interval);
37    }
38
39    /**
40     * Set the bucket sort order.
41     *
42     * @param string $order     "_count", "_term", or the name of a sub-aggregation or sub-aggregation response field
43     * @param string $direction "asc" or "desc"
44     *
45     * @return $this
46     */
47    public function setOrder(string $order, string $direction): self
48    {
49        return $this->setParam('order', [$order => $direction]);
50    }
51
52    /**
53     * Set the minimum number of documents which must fall into a bucket in order for the bucket to be returned.
54     *
55     * @param int $count set to 0 to include empty buckets
56     *
57     * @return $this
58     */
59    public function setMinimumDocumentCount(int $count): self
60    {
61        return $this->setParam('min_doc_count', $count);
62    }
63}
64