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 Match extends AbstractQuery
14{
15    const OPERATOR_OR = 'or';
16    const OPERATOR_AND = 'and';
17
18    const ZERO_TERM_NONE = 'none';
19    const ZERO_TERM_ALL = 'all';
20
21    const FUZZINESS_AUTO = 'AUTO';
22
23    /**
24     * @param string $field
25     * @param mixed  $values
26     */
27    public function __construct(string $field = null, $values = null)
28    {
29        if (null !== $field && null !== $values) {
30            $this->setParam($field, $values);
31        }
32    }
33
34    /**
35     * Sets a param for the message array.
36     *
37     * @param string $field
38     * @param mixed  $values
39     *
40     * @return $this
41     */
42    public function setField(string $field, $values): self
43    {
44        return $this->setParam($field, $values);
45    }
46
47    /**
48     * Sets a param for the given field.
49     *
50     * @param string $field
51     * @param string $key
52     * @param string $value
53     *
54     * @return $this
55     */
56    public function setFieldParam(string $field, string $key, string $value): self
57    {
58        if (!isset($this->_params[$field])) {
59            $this->_params[$field] = [];
60        }
61
62        $this->_params[$field][$key] = $value;
63
64        return $this;
65    }
66
67    /**
68     * Sets the query string.
69     *
70     * @param string $field
71     * @param string $query
72     *
73     * @return $this
74     */
75    public function setFieldQuery(string $field, string $query): self
76    {
77        return $this->setFieldParam($field, 'query', $query);
78    }
79
80    /**
81     * Set field operator.
82     *
83     * @param string $field
84     * @param string $operator
85     *
86     * @return $this
87     */
88    public function setFieldOperator(string $field, string $operator = self::OPERATOR_OR): self
89    {
90        return $this->setFieldParam($field, 'operator', $operator);
91    }
92
93    /**
94     * Set field analyzer.
95     *
96     * @param string $field
97     * @param string $analyzer
98     *
99     * @return $this
100     */
101    public function setFieldAnalyzer(string $field, string $analyzer): self
102    {
103        return $this->setFieldParam($field, 'analyzer', $analyzer);
104    }
105
106    /**
107     * Set field boost value.
108     *
109     * If not set, defaults to 1.0.
110     *
111     * @param string $field
112     * @param float  $boost
113     *
114     * @return $this
115     */
116    public function setFieldBoost(string $field, float $boost = 1.0): self
117    {
118        return $this->setFieldParam($field, 'boost', $boost);
119    }
120
121    /**
122     * Set field minimum should match.
123     *
124     * @param string     $field
125     * @param int|string $minimumShouldMatch
126     *
127     * @return $this
128     *
129     * @see Possible values for minimum_should_match https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-minimum-should-match.html
130     */
131    public function setFieldMinimumShouldMatch(string $field, $minimumShouldMatch): self
132    {
133        return $this->setFieldParam($field, 'minimum_should_match', $minimumShouldMatch);
134    }
135
136    /**
137     * Set field fuzziness.
138     *
139     * @param string $field
140     * @param mixed  $fuzziness
141     *
142     * @return $this
143     */
144    public function setFieldFuzziness(string $field, $fuzziness): self
145    {
146        return $this->setFieldParam($field, 'fuzziness', $fuzziness);
147    }
148
149    /**
150     * Set field fuzzy rewrite.
151     *
152     * @param string $field
153     * @param string $fuzzyRewrite
154     *
155     * @return $this
156     */
157    public function setFieldFuzzyRewrite(string $field, string $fuzzyRewrite): self
158    {
159        return $this->setFieldParam($field, 'fuzzy_rewrite', $fuzzyRewrite);
160    }
161
162    /**
163     * Set field prefix length.
164     *
165     * @param string $field
166     * @param int    $prefixLength
167     *
168     * @return $this
169     */
170    public function setFieldPrefixLength(string $field, int $prefixLength): self
171    {
172        return $this->setFieldParam($field, 'prefix_length', $prefixLength);
173    }
174
175    /**
176     * Set field max expansions.
177     *
178     * @param string $field
179     * @param int    $maxExpansions
180     *
181     * @return $this
182     */
183    public function setFieldMaxExpansions(string $field, int $maxExpansions): self
184    {
185        return $this->setFieldParam($field, 'max_expansions', $maxExpansions);
186    }
187
188    /**
189     * Set zero terms query.
190     *
191     * If not set, default to 'none'
192     *
193     * @param string $field
194     * @param string $zeroTermQuery
195     *
196     * @return $this
197     */
198    public function setFieldZeroTermsQuery(string $field, string $zeroTermQuery = self::ZERO_TERM_NONE): self
199    {
200        return $this->setFieldParam($field, 'zero_terms_query', $zeroTermQuery);
201    }
202
203    /**
204     * Set cutoff frequency.
205     *
206     * @param string $field
207     * @param float  $cutoffFrequency
208     *
209     * @return $this
210     */
211    public function setFieldCutoffFrequency(string $field, float $cutoffFrequency): self
212    {
213        return $this->setFieldParam($field, 'cutoff_frequency', $cutoffFrequency);
214    }
215}
216