1<?php
2
3namespace Elastica\Query;
4
5/**
6 * QueryString query.
7 *
8 * @author   Nicolas Ruflin <spam@ruflin.com>, Jasper van Wanrooy <jasper@vanwanrooy.net>
9 *
10 * @see     https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html
11 */
12class QueryString extends AbstractQuery
13{
14    /**
15     * Query string.
16     *
17     * @var string Query string
18     */
19    protected $_queryString;
20
21    /**
22     * Creates query string object. Calls setQuery with argument.
23     *
24     * @param string $queryString OPTIONAL Query string for object
25     */
26    public function __construct(string $queryString = '')
27    {
28        $this->setQuery($queryString);
29    }
30
31    /**
32     * Sets a new query string for the object.
33     *
34     * @param string $query Query string
35     *
36     * @return $this
37     */
38    public function setQuery(string $query = ''): self
39    {
40        return $this->setParam('query', $query);
41    }
42
43    /**
44     * Sets the default field.
45     * You cannot set fields and default_field.
46     *
47     * If no field is set, _all is chosen
48     *
49     * @param string $field Field
50     *
51     * @return $this
52     */
53    public function setDefaultField(string $field): self
54    {
55        return $this->setParam('default_field', $field);
56    }
57
58    /**
59     * Sets the default operator AND or OR.
60     *
61     * If no operator is set, OR is chosen
62     *
63     * @param string $operator Operator
64     *
65     * @return $this
66     */
67    public function setDefaultOperator(string $operator = 'or'): self
68    {
69        return $this->setParam('default_operator', $operator);
70    }
71
72    /**
73     * Sets the analyzer to analyze the query with.
74     *
75     * @param string $analyzer Analyser to use
76     *
77     * @return $this
78     */
79    public function setAnalyzer(string $analyzer): self
80    {
81        return $this->setParam('analyzer', $analyzer);
82    }
83
84    /**
85     * Sets the parameter to allow * and ? as first characters.
86     *
87     * If not set, defaults to true.
88     *
89     * @param bool $allow
90     *
91     * @return $this
92     */
93    public function setAllowLeadingWildcard(bool $allow = true): self
94    {
95        return $this->setParam('allow_leading_wildcard', $allow);
96    }
97
98    /**
99     * Sets the parameter to enable the position increments in result queries.
100     *
101     * If not set, defaults to true.
102     *
103     * @param bool $enabled
104     *
105     * @return $this
106     */
107    public function setEnablePositionIncrements(bool $enabled = true): self
108    {
109        return $this->setParam('enable_position_increments', $enabled);
110    }
111
112    /**
113     * Sets the fuzzy prefix length parameter.
114     *
115     * If not set, defaults to 0.
116     *
117     * @param int $length
118     *
119     * @return $this
120     */
121    public function setFuzzyPrefixLength(int $length = 0): self
122    {
123        return $this->setParam('fuzzy_prefix_length', $length);
124    }
125
126    /**
127     * Sets the fuzzy minimal similarity parameter.
128     *
129     * If not set, defaults to 0.5
130     *
131     * @param float $minSim
132     *
133     * @return $this
134     */
135    public function setFuzzyMinSim(float $minSim = 0.5): self
136    {
137        return $this->setParam('fuzzy_min_sim', $minSim);
138    }
139
140    /**
141     * Sets the phrase slop.
142     *
143     * If zero, exact phrases are required.
144     * If not set, defaults to zero.
145     *
146     * @param int $phraseSlop
147     *
148     * @return $this
149     */
150    public function setPhraseSlop(int $phraseSlop = 0): self
151    {
152        return $this->setParam('phrase_slop', $phraseSlop);
153    }
154
155    /**
156     * Sets the boost value of the query.
157     *
158     * If not set, defaults to 1.0.
159     *
160     * @param float $boost
161     *
162     * @return $this
163     */
164    public function setBoost(float $boost = 1.0): self
165    {
166        return $this->setParam('boost', $boost);
167    }
168
169    /**
170     * Allows analyzing of wildcard terms.
171     *
172     * If not set, defaults to true
173     *
174     * @param bool $analyze
175     *
176     * @return $this
177     */
178    public function setAnalyzeWildcard(bool $analyze = true): self
179    {
180        return $this->setParam('analyze_wildcard', $analyze);
181    }
182
183    /**
184     * Sets the fields. If no fields are set, _all is chosen.
185     * You cannot set fields and default_field.
186     *
187     * @param array $fields Fields
188     *
189     * @return $this
190     */
191    public function setFields(array $fields): self
192    {
193        return $this->setParam('fields', $fields);
194    }
195
196    /**
197     * Whether to use bool or dis_max queries to internally combine results for multi field search.
198     *
199     * @param bool $value Determines whether to use
200     *
201     * @return $this
202     */
203    public function setUseDisMax(bool $value = true): self
204    {
205        return $this->setParam('use_dis_max', $value);
206    }
207
208    /**
209     * When using dis_max, the disjunction max tie breaker.
210     *
211     * If not set, defaults to 0.0.
212     *
213     * @param float $tieBreaker
214     *
215     * @return $this
216     */
217    public function setTieBreaker(float $tieBreaker = 0.0): self
218    {
219        return $this->setParam('tie_breaker', $tieBreaker);
220    }
221
222    /**
223     * Set a re-write condition. See https://github.com/elasticsearch/elasticsearch/issues/1186 for additional information.
224     *
225     * @param string $rewrite
226     *
227     * @return $this
228     */
229    public function setRewrite(string $rewrite = ''): self
230    {
231        return $this->setParam('rewrite', $rewrite);
232    }
233
234    /**
235     * Set timezone option.
236     *
237     * @param string $timezone
238     *
239     * @return $this
240     */
241    public function setTimezone(string $timezone): self
242    {
243        return $this->setParam('time_zone', $timezone);
244    }
245
246    /**
247     * {@inheritdoc}
248     */
249    public function toArray(): array
250    {
251        return ['query_string' => \array_merge(['query' => $this->_queryString], $this->getParams())];
252    }
253}
254