1<?php
2
3namespace Elastica\Query;
4
5/**
6 * Multi Match.
7 *
8 * @author Rodolfo Adhenawer Campagnoli Moraes <adhenawer@gmail.com>
9 * @author Wong Wing Lun <luiges90@gmail.com>
10 * @author Tristan Maindron <tmaindron@gmail.com>
11 *
12 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html
13 */
14class MultiMatch extends AbstractQuery
15{
16    const TYPE_BEST_FIELDS = 'best_fields';
17    const TYPE_MOST_FIELDS = 'most_fields';
18    const TYPE_CROSS_FIELDS = 'cross_fields';
19    const TYPE_PHRASE = 'phrase';
20    const TYPE_PHRASE_PREFIX = 'phrase_prefix';
21
22    const OPERATOR_OR = 'or';
23    const OPERATOR_AND = 'and';
24
25    const ZERO_TERM_NONE = 'none';
26    const ZERO_TERM_ALL = 'all';
27
28    const FUZZINESS_AUTO = 'AUTO';
29
30    /**
31     * Sets the query.
32     *
33     * @param string $query Query
34     *
35     * @return $this
36     */
37    public function setQuery(string $query = ''): self
38    {
39        return $this->setParam('query', $query);
40    }
41
42    /**
43     * Sets Fields to be used in the query.
44     *
45     * @param array $fields Fields
46     *
47     * @return $this
48     */
49    public function setFields(array $fields = []): self
50    {
51        return $this->setParam('fields', $fields);
52    }
53
54    /**
55     * Sets use dis max indicating to either create a dis_max query or a bool query.
56     *
57     * If not set, defaults to true.
58     *
59     * @param bool $useDisMax
60     *
61     * @return $this
62     */
63    public function setUseDisMax(bool $useDisMax = true): self
64    {
65        return $this->setParam('use_dis_max', $useDisMax);
66    }
67
68    /**
69     * Sets tie breaker to multiplier value to balance the scores between lower and higher scoring fields.
70     *
71     * If not set, defaults to 0.0.
72     *
73     * @param float $tieBreaker
74     *
75     * @return $this
76     */
77    public function setTieBreaker(float $tieBreaker = 0.0): self
78    {
79        return $this->setParam('tie_breaker', $tieBreaker);
80    }
81
82    /**
83     * Sets operator for Match Query.
84     *
85     * If not set, defaults to 'or'
86     *
87     * @param string $operator
88     *
89     * @return $this
90     */
91    public function setOperator(string $operator = self::OPERATOR_OR): self
92    {
93        return $this->setParam('operator', $operator);
94    }
95
96    /**
97     * Set field minimum should match for Match Query.
98     *
99     * @param mixed $minimumShouldMatch
100     *
101     * @return $this
102     */
103    public function setMinimumShouldMatch($minimumShouldMatch): self
104    {
105        return $this->setParam('minimum_should_match', $minimumShouldMatch);
106    }
107
108    /**
109     * Set zero terms query for Match Query.
110     *
111     * If not set, default to 'none'
112     *
113     * @param string $zeroTermQuery
114     *
115     * @return $this
116     */
117    public function setZeroTermsQuery(string $zeroTermQuery = self::ZERO_TERM_NONE): self
118    {
119        return $this->setParam('zero_terms_query', $zeroTermQuery);
120    }
121
122    /**
123     * Set cutoff frequency for Match Query.
124     *
125     * @param float $cutoffFrequency
126     *
127     * @return $this
128     */
129    public function setCutoffFrequency(float $cutoffFrequency): self
130    {
131        return $this->setParam('cutoff_frequency', $cutoffFrequency);
132    }
133
134    /**
135     * Set type.
136     *
137     * @param string $type
138     *
139     * @return $this
140     */
141    public function setType(string $type): self
142    {
143        return $this->setParam('type', $type);
144    }
145
146    /**
147     * Set fuzziness.
148     *
149     * @param float|string $fuzziness
150     *
151     * @return $this
152     */
153    public function setFuzziness($fuzziness): self
154    {
155        return $this->setParam('fuzziness', $fuzziness);
156    }
157
158    /**
159     * Set prefix length.
160     *
161     * @param int $prefixLength
162     *
163     * @return $this
164     */
165    public function setPrefixLength(int $prefixLength): self
166    {
167        return $this->setParam('prefix_length', $prefixLength);
168    }
169
170    /**
171     * Set max expansions.
172     *
173     * @param int $maxExpansions
174     *
175     * @return $this
176     */
177    public function setMaxExpansions(int $maxExpansions): self
178    {
179        return $this->setParam('max_expansions', $maxExpansions);
180    }
181
182    /**
183     * Set analyzer.
184     *
185     * @param string $analyzer
186     *
187     * @return $this
188     */
189    public function setAnalyzer(string $analyzer): self
190    {
191        return $this->setParam('analyzer', $analyzer);
192    }
193}
194