summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/lib/Elastica/Query/QueryString.php
blob: 89ea77cde8a1cb51ec8c36414924905f7cb9f77b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
namespace Elastica\Query;

use Elastica\Exception\InvalidException;

/**
 * QueryString query.
 *
 * @author   Nicolas Ruflin <spam@ruflin.com>, Jasper van Wanrooy <jasper@vanwanrooy.net>
 *
 * @link     http://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html
 */
class QueryString extends AbstractQuery
{
    /**
     * Query string.
     *
     * @var string Query string
     */
    protected $_queryString = '';

    /**
     * Creates query string object. Calls setQuery with argument.
     *
     * @param string $queryString OPTIONAL Query string for object
     */
    public function __construct($queryString = '')
    {
        $this->setQuery($queryString);
    }

    /**
     * Sets a new query string for the object.
     *
     * @param string $query Query string
     *
     * @throws \Elastica\Exception\InvalidException If given parameter is not a string
     *
     * @return $this
     */
    public function setQuery($query = '')
    {
        if (!is_string($query)) {
            throw new InvalidException('Parameter has to be a string');
        }

        return $this->setParam('query', $query);
    }

    /**
     * Sets the default field.
     *
     * If no field is set, _all is chosen
     *
     * @param string $field Field
     *
     * @return $this
     */
    public function setDefaultField($field)
    {
        return $this->setParam('default_field', $field);
    }

    /**
     * Sets the default operator AND or OR.
     *
     * If no operator is set, OR is chosen
     *
     * @param string $operator Operator
     *
     * @return $this
     */
    public function setDefaultOperator($operator)
    {
        return $this->setParam('default_operator', $operator);
    }

    /**
     * Sets the analyzer to analyze the query with.
     *
     * @param string $analyzer Analyser to use
     *
     * @return $this
     */
    public function setAnalyzer($analyzer)
    {
        return $this->setParam('analyzer', $analyzer);
    }

    /**
     * Sets the parameter to allow * and ? as first characters.
     *
     * If not set, defaults to true.
     *
     * @param bool $allow
     *
     * @return $this
     */
    public function setAllowLeadingWildcard($allow = true)
    {
        return $this->setParam('allow_leading_wildcard', (bool) $allow);
    }

    /**
     * Sets the parameter to enable the position increments in result queries.
     *
     * If not set, defaults to true.
     *
     * @param bool $enabled
     *
     * @return $this
     */
    public function setEnablePositionIncrements($enabled = true)
    {
        return $this->setParam('enable_position_increments', (bool) $enabled);
    }

    /**
     * Sets the fuzzy prefix length parameter.
     *
     * If not set, defaults to 0.
     *
     * @param int $length
     *
     * @return $this
     */
    public function setFuzzyPrefixLength($length = 0)
    {
        return $this->setParam('fuzzy_prefix_length', (int) $length);
    }

    /**
     * Sets the fuzzy minimal similarity parameter.
     *
     * If not set, defaults to 0.5
     *
     * @param float $minSim
     *
     * @return $this
     */
    public function setFuzzyMinSim($minSim = 0.5)
    {
        return $this->setParam('fuzzy_min_sim', (float) $minSim);
    }

    /**
     * Sets the phrase slop.
     *
     * If zero, exact phrases are required.
     * If not set, defaults to zero.
     *
     * @param int $phraseSlop
     *
     * @return $this
     */
    public function setPhraseSlop($phraseSlop = 0)
    {
        return $this->setParam('phrase_slop', (int) $phraseSlop);
    }

    /**
     * Sets the boost value of the query.
     *
     * If not set, defaults to 1.0.
     *
     * @param float $boost
     *
     * @return $this
     */
    public function setBoost($boost = 1.0)
    {
        return $this->setParam('boost', (float) $boost);
    }

    /**
     * Allows analyzing of wildcard terms.
     *
     * If not set, defaults to true
     *
     * @param bool $analyze
     *
     * @return $this
     */
    public function setAnalyzeWildcard($analyze = true)
    {
        return $this->setParam('analyze_wildcard', (bool) $analyze);
    }

    /**
     * Sets the param to automatically generate phrase queries.
     *
     * If not set, defaults to true.
     *
     * @param bool $autoGenerate
     *
     * @return $this
     */
    public function setAutoGeneratePhraseQueries($autoGenerate = true)
    {
        return $this->setParam('auto_generate_phrase_queries', (bool) $autoGenerate);
    }

    /**
     * Sets the fields. If no fields are set, _all is chosen.
     *
     * @param array $fields Fields
     *
     * @throws \Elastica\Exception\InvalidException If given parameter is not an array
     *
     * @return $this
     */
    public function setFields(array $fields)
    {
        if (!is_array($fields)) {
            throw new InvalidException('Parameter has to be an array');
        }

        return $this->setParam('fields', $fields);
    }

    /**
     * Whether to use bool or dis_max queries to internally combine results for multi field search.
     *
     * @param bool $value Determines whether to use
     *
     * @return $this
     */
    public function setUseDisMax($value = true)
    {
        return $this->setParam('use_dis_max', (bool) $value);
    }

    /**
     * When using dis_max, the disjunction max tie breaker.
     *
     * If not set, defaults to 0.
     *
     * @param int $tieBreaker
     *
     * @return $this
     */
    public function setTieBreaker($tieBreaker = 0)
    {
        return $this->setParam('tie_breaker', (float) $tieBreaker);
    }

    /**
     * Set a re-write condition. See https://github.com/elasticsearch/elasticsearch/issues/1186 for additional information.
     *
     * @param string $rewrite
     *
     * @return $this
     */
    public function setRewrite($rewrite = '')
    {
        return $this->setParam('rewrite', $rewrite);
    }

    /**
     * Set timezone option.
     *
     * @param string $timezone
     *
     * @return $this
     */
    public function setTimezone($timezone)
    {
        return $this->setParam('time_zone', $timezone);
    }

    /**
     * Converts query to array.
     *
     * @see \Elastica\Query\AbstractQuery::toArray()
     *
     * @return array Query array
     */
    public function toArray()
    {
        return array('query_string' => array_merge(array('query' => $this->_queryString), $this->getParams()));
    }
}