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