1<?php
2
3namespace Elastica\Query;
4
5/**
6 * Class Common.
7 *
8 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-common-terms-query.html
9 */
10class Common extends AbstractQuery
11{
12    const OPERATOR_AND = 'and';
13    const OPERATOR_OR = 'or';
14
15    /**
16     * @var string
17     */
18    protected $_field;
19
20    /**
21     * @var array
22     */
23    protected $_queryParams = [];
24
25    /**
26     * @param string $field           the field on which to query
27     * @param string $query           the query string
28     * @param float  $cutoffFrequency percentage in decimal form (.001 == 0.1%)
29     */
30    public function __construct(string $field, string $query, float $cutoffFrequency)
31    {
32        $this->setField($field);
33        $this->setQuery($query);
34        $this->setCutoffFrequency($cutoffFrequency);
35    }
36
37    /**
38     * Set the field on which to query.
39     *
40     * @param string $field the field on which to query
41     *
42     * @return $this
43     */
44    public function setField(string $field): self
45    {
46        $this->_field = $field;
47
48        return $this;
49    }
50
51    /**
52     * Set the query string for this query.
53     *
54     * @param string $query
55     *
56     * @return $this
57     */
58    public function setQuery(string $query): self
59    {
60        return $this->setQueryParam('query', $query);
61    }
62
63    /**
64     * Set the frequency below which terms will be put in the low frequency group.
65     *
66     * @param float $frequency percentage in decimal form (.001 == 0.1%)
67     *
68     * @return $this
69     */
70    public function setCutoffFrequency(float $frequency): self
71    {
72        return $this->setQueryParam('cutoff_frequency', $frequency);
73    }
74
75    /**
76     * Set the logic operator for low frequency terms.
77     *
78     * @param string $operator see OPERATOR_* class constants for options
79     *
80     * @return $this
81     */
82    public function setLowFrequencyOperator(string $operator = self::OPERATOR_OR): self
83    {
84        return $this->setQueryParam('low_freq_operator', $operator);
85    }
86
87    /**
88     * Set the logic operator for high frequency terms.
89     *
90     * @param string $operator see OPERATOR_* class constants for options
91     *
92     * @return $this
93     */
94    public function setHighFrequencyOperator(string $operator = self::OPERATOR_OR): self
95    {
96        return $this->setQueryParam('high_frequency_operator', $operator);
97    }
98
99    /**
100     * Set the minimum_should_match parameter.
101     *
102     * @param int|string $minimum minimum number of low frequency terms which must be present
103     *
104     * @return $this
105     *
106     * @see Possible values for minimum_should_match https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-minimum-should-match.html
107     */
108    public function setMinimumShouldMatch($minimum): self
109    {
110        return $this->setQueryParam('minimum_should_match', $minimum);
111    }
112
113    /**
114     * Set the boost for this query.
115     *
116     * @param float $boost
117     *
118     * @return $this
119     */
120    public function setBoost(float $boost): self
121    {
122        return $this->setQueryParam('boost', $boost);
123    }
124
125    /**
126     * Set the analyzer for this query.
127     *
128     * @param string $analyzer
129     *
130     * @return $this
131     */
132    public function setAnalyzer(string $analyzer): self
133    {
134        return $this->setQueryParam('analyzer', $analyzer);
135    }
136
137    /**
138     * Set a parameter in the body of this query.
139     *
140     * @param string $key   parameter key
141     * @param mixed  $value parameter value
142     *
143     * @return $this
144     */
145    public function setQueryParam(string $key, $value): self
146    {
147        $this->_queryParams[$key] = $value;
148
149        return $this;
150    }
151
152    /**
153     * {@inheritdoc}
154     */
155    public function toArray(): array
156    {
157        $this->setParam($this->_field, $this->_queryParams);
158
159        return parent::toArray();
160    }
161}
162