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    /**
13     * @param string     $name     the name of this aggregation
14     * @param string     $field    the name of the field on which to perform the aggregation
15     * @param int|string $interval the interval by which documents will be bucketed
16     */
17    public function __construct(string $name, string $field, $interval)
18    {
19        parent::__construct($name);
20        $this->setField($field);
21        $this->setInterval($interval);
22    }
23
24    /**
25     * Set the interval by which documents will be bucketed.
26     *
27     * @param int|string $interval
28     *
29     * @return $this
30     */
31    public function setInterval($interval): self
32    {
33        return $this->setParam('interval', $interval);
34    }
35
36    /**
37     * Set the bucket sort order.
38     *
39     * @param string $order     "_count", "_term", or the name of a sub-aggregation or sub-aggregation response field
40     * @param string $direction "asc" or "desc"
41     *
42     * @return $this
43     */
44    public function setOrder(string $order, string $direction): self
45    {
46        return $this->setParam('order', [$order => $direction]);
47    }
48
49    /**
50     * Set the minimum number of documents which must fall into a bucket in order for the bucket to be returned.
51     *
52     * @param int $count set to 0 to include empty buckets
53     *
54     * @return $this
55     */
56    public function setMinimumDocumentCount(int $count): self
57    {
58        return $this->setParam('min_doc_count', $count);
59    }
60}
61