Completed
Push — master ( fa6e3b...ec1e30 )
by Nicolas
03:03
created

Query::setMinScore()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
namespace Elastica;
3
4
use Elastica\Aggregation\AbstractAggregation;
5
use Elastica\Exception\InvalidException;
6
use Elastica\Exception\NotImplementedException;
7
use Elastica\Query\AbstractQuery;
8
use Elastica\Query\MatchAll;
9
use Elastica\Query\QueryString;
10
use Elastica\Script\AbstractScript;
11
use Elastica\Script\ScriptFields;
12
use Elastica\Suggest\AbstractSuggest;
13
14
/**
15
 * Elastica query object.
16
 *
17
 * Creates different types of queries
18
 *
19
 * @author Nicolas Ruflin <[email protected]>
20
 *
21
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html
22
 */
23
class Query extends Param
24
{
25
    /**
26
     * Suggest query or not.
27
     *
28
     * @var int Suggest
29
     */
30
    protected $_suggest = 0;
31
32
    /**
33
     * Creates a query object.
34
     *
35
     * @param array|\Elastica\Query\AbstractQuery $query OPTIONAL Query object (default = null)
36
     */
37
    public function __construct($query = null)
38
    {
39
        if (is_array($query)) {
40
            $this->setRawQuery($query);
41
        } elseif ($query instanceof AbstractQuery) {
42
            $this->setQuery($query);
43
        } elseif ($query instanceof Suggest) {
44
            $this->setSuggest($query);
45
        }
46
    }
47
48
    /**
49
     * Transforms a string or an array to a query object.
50
     *
51
     * If query is empty,
52
     *
53
     * @param mixed $query
54
     *
55
     * @throws \Elastica\Exception\NotImplementedException
56
     *
57
     * @return self
58
     */
59
    public static function create($query)
60
    {
61
        switch (true) {
62
            case $query instanceof self:
63
                return $query;
64
            case $query instanceof AbstractQuery:
65
                return new self($query);
66
            case empty($query):
67
                return new self(new MatchAll());
68
            case is_array($query):
69
                return new self($query);
70
            case is_string($query):
71
                return new self(new QueryString($query));
72
            case $query instanceof AbstractSuggest:
73
                return new self(new Suggest($query));
0 ignored issues
show
Documentation introduced by
new \Elastica\Suggest($query) is of type object<Elastica\Suggest>, but the function expects a array|object<Elastica\Query\AbstractQuery>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
75
            case $query instanceof Suggest:
76
                return new self($query);
0 ignored issues
show
Documentation introduced by
$query is of type object<Elastica\Suggest>, but the function expects a array|object<Elastica\Query\AbstractQuery>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
77
78
        }
79
80
        // TODO: Implement queries without
81
        throw new NotImplementedException();
82
    }
83
84
    /**
85
     * Sets query as raw array. Will overwrite all already set arguments.
86
     *
87
     * @param array $query Query array
88
     *
89
     * @return $this
90
     */
91
    public function setRawQuery(array $query)
92
    {
93
        $this->_params = $query;
94
95
        return $this;
96
    }
97
98
    /**
99
     * Sets the query.
100
     *
101
     * @param \Elastica\Query\AbstractQuery $query Query object
102
     *
103
     * @return $this
104
     */
105
    public function setQuery(AbstractQuery $query)
106
    {
107
        return $this->setParam('query', $query);
108
    }
109
110
    /**
111
     * Gets the query object.
112
     *
113
     * @return \Elastica\Query\AbstractQuery
114
     **/
115
    public function getQuery()
116
    {
117
        return $this->getParam('query');
118
    }
119
120
    /**
121
     * Sets the start from which the search results should be returned.
122
     *
123
     * @param int $from
124
     *
125
     * @return $this
126
     */
127
    public function setFrom($from)
128
    {
129
        return $this->setParam('from', $from);
130
    }
131
132
    /**
133
     * Sets sort arguments for the query
134
     * Replaces existing values.
135
     *
136
     * @param array $sortArgs Sorting arguments
137
     *
138
     * @return $this
139
     *
140
     * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html
141
     */
142
    public function setSort(array $sortArgs)
143
    {
144
        return $this->setParam('sort', $sortArgs);
145
    }
146
147
    /**
148
     * Adds a sort param to the query.
149
     *
150
     * @param mixed $sort Sort parameter
151
     *
152
     * @return $this
153
     *
154
     * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html
155
     */
156
    public function addSort($sort)
157
    {
158
        return $this->addParam('sort', $sort);
159
    }
160
161
    /**
162
     * Keep track of the scores when sorting results.
163
     *
164
     * @param bool $trackScores
165
     *
166
     * @return $this
167
     *
168
     * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html#_track_scores
169
     */
170
    public function setTrackScores($trackScores = true)
171
    {
172
        return $this->setParam('track_scores', (bool) $trackScores);
173
    }
174
175
    /**
176
     * Sets highlight arguments for the query.
177
     *
178
     * @param array $highlightArgs Set all highlight arguments
179
     *
180
     * @return $this
181
     *
182
     * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html
183
     */
184
    public function setHighlight(array $highlightArgs)
185
    {
186
        return $this->setParam('highlight', $highlightArgs);
187
    }
188
189
    /**
190
     * Adds a highlight argument.
191
     *
192
     * @param mixed $highlight Add highlight argument
193
     *
194
     * @return $this
195
     *
196
     * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html
197
     */
198
    public function addHighlight($highlight)
199
    {
200
        return $this->addParam('highlight', $highlight);
201
    }
202
203
    /**
204
     * Sets maximum number of results for this query.
205
     *
206
     * @param int $size OPTIONAL Maximal number of results for query (default = 10)
207
     *
208
     * @return $this
209
     */
210
    public function setSize($size = 10)
211
    {
212
        return $this->setParam('size', $size);
213
    }
214
215
    /**
216
     * Enables explain on the query.
217
     *
218
     * @param bool $explain OPTIONAL Enabled or disable explain (default = true)
219
     *
220
     * @return $this
221
     *
222
     * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-explain.html
223
     */
224
    public function setExplain($explain = true)
225
    {
226
        return $this->setParam('explain', $explain);
227
    }
228
229
    /**
230
     * Enables version on the query.
231
     *
232
     * @param bool $version OPTIONAL Enabled or disable version (default = true)
233
     *
234
     * @return $this
235
     *
236
     * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-version.html
237
     */
238
    public function setVersion($version = true)
239
    {
240
        return $this->setParam('version', $version);
241
    }
242
243
    /**
244
     * Sets the fields to be returned by the search
245
     * NOTICE php will encode modified(or named keys) array into object format in json format request
246
     * so the fields array must a sequence(list) type of array.
247
     *
248
     * @param array $fields Fields to be returned
249
     *
250
     * @return $this
251
     *
252
     * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-fields.html
253
     */
254
    public function setStoredFields(array $fields)
255
    {
256
        return $this->setParam('stored_fields', $fields);
257
    }
258
259
    /**
260
     * Sets the fields not stored to be returned by the search.
261
     *
262
     * @param array $fieldDataFields Fields not stored to be returned
263
     *
264
     * @return $this
265
     *
266
     * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-fielddata-fields.html
267
     */
268
    public function setFieldDataFields(array $fieldDataFields)
269
    {
270
        return $this->setParam('fielddata_fields', $fieldDataFields);
271
    }
272
273
    /**
274
     * Set script fields.
275
     *
276
     * @param array|\Elastica\Script\ScriptFields $scriptFields Script fields
277
     *
278
     * @return $this
279
     *
280
     * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-script-fields.html
281
     */
282
    public function setScriptFields($scriptFields)
283
    {
284
        if (is_array($scriptFields)) {
285
            $scriptFields = new ScriptFields($scriptFields);
286
        }
287
288
        return $this->setParam('script_fields', $scriptFields);
289
    }
290
291
    /**
292
     * Adds a Script to the query.
293
     *
294
     * @param string                          $name
295
     * @param \Elastica\Script\AbstractScript $script Script object
296
     *
297
     * @return $this
298
     */
299
    public function addScriptField($name, AbstractScript $script)
300
    {
301
        if (isset($this->_params['script_fields'])) {
302
            $this->_params['script_fields']->addScript($name, $script);
303
        } else {
304
            $this->setScriptFields([$name => $script]);
305
        }
306
307
        return $this;
308
    }
309
310
    /**
311
     * Adds an Aggregation to the query.
312
     *
313
     * @param AbstractAggregation $agg
314
     *
315
     * @return $this
316
     */
317
    public function addAggregation(AbstractAggregation $agg)
318
    {
319
        $this->_params['aggs'][] = $agg;
320
321
        return $this;
322
    }
323
324
    /**
325
     * Converts all query params to an array.
326
     *
327
     * @return array Query array
328
     */
329
    public function toArray()
330
    {
331
        if (!isset($this->_params['query']) && ($this->_suggest == 0)) {
332
            $this->setQuery(new MatchAll());
333
        }
334
335
        if (isset($this->_params['post_filter']) && 0 === count($this->_params['post_filter'])) {
336
            unset($this->_params['post_filter']);
337
        }
338
339
        $array = $this->_convertArrayable($this->_params);
340
341
        if (isset($array['suggest'])) {
342
            $array['suggest'] = $array['suggest']['suggest'];
343
        }
344
345
        return $array;
346
    }
347
348
    /**
349
     * Allows filtering of documents based on a minimum score.
350
     *
351
     * @param float $minScore Minimum score to filter documents by
352
     *
353
     * @throws \Elastica\Exception\InvalidException
354
     *
355
     * @return $this
356
     */
357
    public function setMinScore($minScore)
358
    {
359
        if (!is_numeric($minScore)) {
360
            throw new InvalidException('has to be numeric param');
361
        }
362
363
        return $this->setParam('min_score', $minScore);
364
    }
365
366
    /**
367
     * Add a suggest term.
368
     *
369
     * @param \Elastica\Suggest $suggest suggestion object
370
     *
371
     * @return $this
372
     */
373
    public function setSuggest(Suggest $suggest)
374
    {
375
        $this->setParam('suggest', $suggest);
376
377
        $this->_suggest = 1;
378
379
        return $this;
380
    }
381
382
    /**
383
     * Add a Rescore.
384
     *
385
     * @param mixed $rescore suggestion object
386
     *
387
     * @return $this
388
     */
389
    public function setRescore($rescore)
390
    {
391
        if (is_array($rescore)) {
392
            $buffer = [];
393
394
            foreach ($rescore as $rescoreQuery) {
395
                $buffer [] = $rescoreQuery;
396
            }
397
        } else {
398
            $buffer = $rescore;
399
        }
400
401
        return $this->setParam('rescore', $buffer);
402
    }
403
404
    /**
405
     * Sets the _source field to be returned with every hit.
406
     *
407
     * @param array|bool $params Fields to be returned or false to disable source
408
     *
409
     * @return $this
410
     *
411
     * @link   https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-source-filtering.html
412
     */
413
    public function setSource($params)
414
    {
415
        return $this->setParam('_source', $params);
416
    }
417
418
    /**
419
     * Sets post_filter argument for the query. The filter is applied after the query has executed.
420
     *
421
     * @param array|\Elastica\Query\AbstractQuery $filter
422
     *
423
     * @return $this
424
     *
425
     * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-post-filter.html
426
     */
427
    public function setPostFilter(AbstractQuery $filter)
428
    {
429
        return $this->setParam('post_filter', $filter);
430
    }
431
}
432