1<?php
2
3namespace Elastica\Query;
4
5/**
6 * Match query.
7 *
8 * @author F21
9 * @author WONG Wing Lun <luiges90@gmail.com>
10 *
11 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html
12 */
13class MatchQuery extends AbstractQuery
14{
15    public const OPERATOR_OR = 'or';
16    public const OPERATOR_AND = 'and';
17
18    public const ZERO_TERM_NONE = 'none';
19    public const ZERO_TERM_ALL = 'all';
20
21    public const FUZZINESS_AUTO = 'AUTO';
22
23    /**
24     * @param mixed $values
25     */
26    public function __construct(?string $field = null, $values = null)
27    {
28        if (null !== $field && null !== $values) {
29            $this->setParam($field, $values);
30        }
31    }
32
33    /**
34     * Sets a param for the message array.
35     *
36     * @param mixed $values
37     *
38     * @return $this
39     */
40    public function setField(string $field, $values): self
41    {
42        return $this->setParam($field, $values);
43    }
44
45    /**
46     * Sets a param for the given field.
47     *
48     * @param bool|float|int|string $value
49     *
50     * @return $this
51     */
52    public function setFieldParam(string $field, string $key, $value): self
53    {
54        if (!isset($this->_params[$field])) {
55            $this->_params[$field] = [];
56        }
57
58        $this->_params[$field][$key] = $value;
59
60        return $this;
61    }
62
63    /**
64     * Sets the query string.
65     *
66     * @return $this
67     */
68    public function setFieldQuery(string $field, string $query): self
69    {
70        return $this->setFieldParam($field, 'query', $query);
71    }
72
73    /**
74     * Set field operator.
75     *
76     * @return $this
77     */
78    public function setFieldOperator(string $field, string $operator = self::OPERATOR_OR): self
79    {
80        return $this->setFieldParam($field, 'operator', $operator);
81    }
82
83    /**
84     * Set field analyzer.
85     *
86     * @return $this
87     */
88    public function setFieldAnalyzer(string $field, string $analyzer): self
89    {
90        return $this->setFieldParam($field, 'analyzer', $analyzer);
91    }
92
93    /**
94     * Set field boost value.
95     *
96     * If not set, defaults to 1.0.
97     *
98     * @return $this
99     */
100    public function setFieldBoost(string $field, float $boost = 1.0): self
101    {
102        return $this->setFieldParam($field, 'boost', $boost);
103    }
104
105    /**
106     * Set field minimum should match.
107     *
108     * @param int|string $minimumShouldMatch
109     *
110     * @return $this
111     *
112     * @see Possible values for minimum_should_match https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-minimum-should-match.html
113     */
114    public function setFieldMinimumShouldMatch(string $field, $minimumShouldMatch): self
115    {
116        return $this->setFieldParam($field, 'minimum_should_match', $minimumShouldMatch);
117    }
118
119    /**
120     * Set field fuzziness.
121     *
122     * @param mixed $fuzziness
123     *
124     * @return $this
125     */
126    public function setFieldFuzziness(string $field, $fuzziness): self
127    {
128        return $this->setFieldParam($field, 'fuzziness', $fuzziness);
129    }
130
131    /**
132     * Set field fuzzy rewrite.
133     *
134     * @return $this
135     */
136    public function setFieldFuzzyRewrite(string $field, string $fuzzyRewrite): self
137    {
138        return $this->setFieldParam($field, 'fuzzy_rewrite', $fuzzyRewrite);
139    }
140
141    /**
142     * Set field prefix length.
143     *
144     * @return $this
145     */
146    public function setFieldPrefixLength(string $field, int $prefixLength): self
147    {
148        return $this->setFieldParam($field, 'prefix_length', $prefixLength);
149    }
150
151    /**
152     * Set field max expansions.
153     *
154     * @return $this
155     */
156    public function setFieldMaxExpansions(string $field, int $maxExpansions): self
157    {
158        return $this->setFieldParam($field, 'max_expansions', $maxExpansions);
159    }
160
161    /**
162     * Set zero terms query.
163     *
164     * If not set, default to 'none'
165     *
166     * @return $this
167     */
168    public function setFieldZeroTermsQuery(string $field, string $zeroTermQuery = self::ZERO_TERM_NONE): self
169    {
170        return $this->setFieldParam($field, 'zero_terms_query', $zeroTermQuery);
171    }
172
173    /**
174     * Set cutoff frequency.
175     *
176     * @return $this
177     *
178     * @deprecated since 7.1.3
179     */
180    public function setFieldCutoffFrequency(string $field, float $cutoffFrequency): self
181    {
182        \trigger_deprecation('ruflin/elastica', '7.1.3', 'The "%s()" method is deprecated. It will be removed in 8.0.', __METHOD__);
183
184        return $this->setFieldParam($field, 'cutoff_frequency', $cutoffFrequency);
185    }
186}
187