1<?php
2
3namespace Elastica\Aggregation;
4
5/**
6 * Class AbstractTermsAggregation.
7 */
8abstract class AbstractTermsAggregation extends AbstractSimpleAggregation
9{
10    use Traits\ShardSizeTrait;
11
12    public const EXECUTION_HINT_MAP = 'map';
13    public const EXECUTION_HINT_GLOBAL_ORDINALS = 'global_ordinals';
14
15    /**
16     * Set the minimum number of documents in which a term must appear in order to be returned in a bucket.
17     *
18     * @return $this
19     */
20    public function setMinimumDocumentCount(int $count): self
21    {
22        return $this->setParam('min_doc_count', $count);
23    }
24
25    /**
26     * Filter documents to include based on a regular expression.
27     *
28     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/regexp-syntax.html for syntax
29     *
30     * @param string $pattern a regular expression, following the Regexp syntax
31     *
32     * @return $this
33     */
34    public function setInclude(string $pattern): self
35    {
36        return $this->setParam('include', $pattern);
37    }
38
39    /**
40     * Filter documents to include based on a list of exact values.
41     *
42     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_filtering_values_with_exact_values_2
43     *
44     * @param string[] $values
45     *
46     * @return $this
47     */
48    public function setIncludeAsExactMatch(array $values): self
49    {
50        return $this->setParam('include', $values);
51    }
52
53    /**
54     * Set the aggregation filter to use partitions.
55     *
56     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_filtering_values_with_partitions
57     *
58     * @return $this
59     */
60    public function setIncludeWithPartitions(int $partition, int $numPartitions): self
61    {
62        return $this->setParam('include', [
63            'partition' => $partition,
64            'num_partitions' => $numPartitions,
65        ]);
66    }
67
68    /**
69     * Filter documents to exclude based on a regular expression.
70     *
71     * @param string $pattern a regular expression
72     *
73     * @return $this
74     */
75    public function setExclude(string $pattern): self
76    {
77        return $this->setParam('exclude', $pattern);
78    }
79
80    /**
81     * Filter documents to exclude based on a list of exact values.
82     *
83     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_filtering_values_with_exact_values_2
84     *
85     * @param string[] $values
86     *
87     * @return $this
88     */
89    public function setExcludeAsExactMatch(array $values): self
90    {
91        return $this->setParam('exclude', $values);
92    }
93
94    /**
95     * Sets the amount of terms to be returned.
96     *
97     * @return $this
98     */
99    public function setSize(int $size): self
100    {
101        return $this->setParam('size', $size);
102    }
103
104    /**
105     * Instruct Elasticsearch to use direct field data or ordinals of the field values to execute this aggregation.
106     * The execution hint will be ignored if it is not applicable.
107     *
108     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-execution-hint
109     *
110     * @param string $hint Execution hint, use one of self::EXECUTION_HINT_MAP or self::EXECUTION_HINT_GLOBAL_ORDINALS
111     *
112     * @return $this
113     */
114    public function setExecutionHint(string $hint): self
115    {
116        return $this->setParam('execution_hint', $hint);
117    }
118}
119