Completed
Pull Request — master (#306)
by
unknown
01:52
created

Search   D

Complexity

Total Complexity 58

Size/Duplication

Total Lines 810
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 58
lcom 1
cbo 10
dl 0
loc 810
rs 4.35
c 0
b 0
f 0

53 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __wakeup() 0 4 1
A initializeSerializer() 0 11 2
A destroyEndpoint() 0 4 1
A addQuery() 0 7 1
A getEndpoint() 0 8 2
A getQueries() 0 6 1
A setQueryParameters() 0 6 1
A setEndpointParameters() 0 8 1
A addPostFilter() 0 8 1
A getPostFilters() 0 6 1
A setPostFilterParameters() 0 6 1
A addAggregation() 0 6 1
A getAggregations() 0 4 1
A addInnerHit() 0 6 1
A getInnerHits() 0 4 1
A addSort() 0 6 1
A getSorts() 0 4 1
A addHighlight() 0 6 1
A getHighlights() 0 7 1
A addSuggest() 0 6 1
A getSuggests() 0 4 1
A getFrom() 0 4 1
A setFrom() 0 6 1
A setTerminateAfter() 0 6 1
A setTimeout() 0 6 1
A isTrackTotalHits() 0 4 1
A setTrackTotalHits() 0 6 1
A getSize() 0 4 1
A setSize() 0 6 1
A isSource() 0 4 1
A setSource() 0 6 1
A getStoredFields() 0 4 1
A setStoredFields() 0 6 1
A getScriptFields() 0 4 1
A setScriptFields() 0 6 1
A getDocValueFields() 0 4 1
A setDocValueFields() 0 6 1
A isExplain() 0 4 1
A setExplain() 0 6 1
A isVersion() 0 4 1
A setVersion() 0 6 1
A getIndicesBoost() 0 4 1
A setIndicesBoost() 0 6 1
A getMinScore() 0 4 1
A setMinScore() 0 6 1
A getSearchAfter() 0 4 1
A setSearchAfter() 0 6 1
A getScroll() 0 4 1
A setScroll() 0 8 1
A addUriParam() 0 35 2
A getUriParams() 0 4 1
A toArray() 0 29 3

How to fix   Complexity   

Complex Class

Complex classes like Search often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Search, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\ElasticsearchDSL;
13
14
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
15
use ONGR\ElasticsearchDSL\Highlight\Highlight;
16
use ONGR\ElasticsearchDSL\InnerHit\NestedInnerHit;
17
use ONGR\ElasticsearchDSL\Query\Compound\BoolQuery;
18
use ONGR\ElasticsearchDSL\SearchEndpoint\AbstractSearchEndpoint;
19
use ONGR\ElasticsearchDSL\SearchEndpoint\AggregationsEndpoint;
20
use ONGR\ElasticsearchDSL\SearchEndpoint\HighlightEndpoint;
21
use ONGR\ElasticsearchDSL\SearchEndpoint\InnerHitsEndpoint;
22
use ONGR\ElasticsearchDSL\SearchEndpoint\PostFilterEndpoint;
23
use ONGR\ElasticsearchDSL\SearchEndpoint\QueryEndpoint;
24
use ONGR\ElasticsearchDSL\SearchEndpoint\SearchEndpointFactory;
25
use ONGR\ElasticsearchDSL\SearchEndpoint\SearchEndpointInterface;
26
use ONGR\ElasticsearchDSL\SearchEndpoint\SortEndpoint;
27
use ONGR\ElasticsearchDSL\SearchEndpoint\SuggestEndpoint;
28
use ONGR\ElasticsearchDSL\Serializer\Normalizer\CustomReferencedNormalizer;
29
use ONGR\ElasticsearchDSL\Serializer\OrderedSerializer;
30
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
31
32
/**
33
 * Search object that can be executed by a manager.
34
 */
35
class Search
36
{
37
    /**
38
     * If you don’t need to track the total number of hits at all you can improve
39
     * query times by setting this option to false. Defaults to true.
40
     *
41
     * @var bool
42
     */
43
    private $trackTotalHits;
44
45
    /**
46
     * To retrieve hits from a certain offset. Defaults to 0.
47
     *
48
     * @var int
49
     */
50
    private $from;
51
52
    /**
53
     * The number of hits to return. Defaults to 10. If you do not care about getting some
54
     * hits back but only about the number of matches and/or aggregations, setting the value
55
     * to 0 will help performance.
56
     *
57
     * @var int
58
     */
59
    private $size;
60
61
    /**
62
     * The maximum number of documents to collect for each shard, upon reaching which the query execution will terminate early.
63
     *
64
     * @var integer
65
     */
66
    private $terminateAfter;
67
68
    /**
69
     * Explicit timeout for each search request. Defaults to no timeout.
70
     *
71
     * @var time units
72
     */
73
    private $timeout;
74
75
    /**
76
     * Allows to control how the _source field is returned with every hit. By default
77
     * operations return the contents of the _source field unless you have used the
78
     * stored_fields parameter or if the _source field is disabled.
79
     *
80
     * @var bool
81
     */
82
    private $source;
83
84
    /**
85
     * Allows to selectively load specific stored fields for each document represented by a search hit.
86
     *
87
     * @var array
88
     */
89
    private $storedFields;
90
91
    /**
92
     * Allows to return a script evaluation (based on different fields) for each hit.
93
     * Script fields can work on fields that are not stored, and allow to return custom
94
     * values to be returned (the evaluated value of the script). Script fields can
95
     * also access the actual _source document indexed and extract specific elements
96
     * to be returned from it (can be an "object" type).
97
     *
98
     * @var array
99
     */
100
    private $scriptFields;
101
102
    /**
103
     * Allows to return the doc value representation of a field for each hit. Doc value
104
     * fields can work on fields that are not stored. Note that if the fields parameter
105
     * specifies fields without docvalues it will try to load the value from the fielddata
106
     * cache causing the terms for that field to be loaded to memory (cached), which will
107
     * result in more memory consumption.
108
     *
109
     * @var array
110
     */
111
    private $docValueFields;
112
113
    /**
114
     * Enables explanation for each hit on how its score was computed.
115
     *
116
     * @var bool
117
     */
118
    private $explain;
119
120
    /**
121
     * Returns a version for each search hit.
122
     *
123
     * @var bool
124
     */
125
    private $version;
126
127
    /**
128
     * Allows to configure different boost level per index when searching across more
129
     * than one indices. This is very handy when hits coming from one index matter more
130
     * than hits coming from another index (think social graph where each user has an index).
131
     *
132
     * @var array
133
     */
134
    private $indicesBoost;
135
136
    /**
137
     * Exclude documents which have a _score less than the minimum specified in min_score.
138
     *
139
     * @var int
140
     */
141
    private $minScore;
142
143
    /**
144
     * Pagination of results can be done by using the from and size but the cost becomes
145
     * prohibitive when the deep pagination is reached. The index.max_result_window which
146
     * defaults to 10,000 is a safeguard, search requests take heap memory and time
147
     * proportional to from + size. The Scroll api is recommended for efficient deep
148
     * scrolling but scroll contexts are costly and it is not recommended to use it for
149
     * real time user requests. The search_after parameter circumvents this problem by
150
     * providing a live cursor. The idea is to use the results from the previous page to
151
     * help the retrieval of the next page.
152
     *
153
     * @var array
154
     */
155
    private $searchAfter;
156
157
    /**
158
     * URI parameters alongside Request body search.
159
     *
160
     * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-uri-request.html
161
     *
162
     * @var array
163
     */
164
    private $uriParams = [];
165
166
    /**
167
     * While a search request returns a single “page” of results, the scroll API can be used to retrieve
168
     * large numbers of results (or even all results) from a single search request, in much the same way
169
     * as you would use a cursor on a traditional database. Scrolling is not intended for real time user
170
     * requests, but rather for processing large amounts of data, e.g. in order to reindex the contents
171
     * of one index into a new index with a different configuration.
172
     *
173
     * @var string
174
     */
175
    private $scroll;
176
177
    /**
178
     * @var OrderedSerializer
179
     */
180
    private static $serializer;
181
182
    /**
183
     * @var SearchEndpointInterface[]
184
     */
185
    private $endpoints = [];
186
187
    /**
188
     * Constructor to initialize static properties
189
     */
190
    public function __construct()
191
    {
192
        $this->initializeSerializer();
193
    }
194
195
    /**
196
     * Wakeup method to initialize static properties
197
     */
198
    public function __wakeup()
199
    {
200
        $this->initializeSerializer();
201
    }
202
203
    /**
204
     * Initializes the serializer
205
     */
206
    private function initializeSerializer()
207
    {
208
        if (static::$serializer === null) {
0 ignored issues
show
Bug introduced by
Since $serializer is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $serializer to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
209
            static::$serializer = new OrderedSerializer(
0 ignored issues
show
Bug introduced by
Since $serializer is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $serializer to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
210
                [
211
                    new CustomReferencedNormalizer(),
212
                    new CustomNormalizer(),
213
                ]
214
            );
215
        }
216
    }
217
218
    /**
219
     * Destroys search endpoint.
220
     *
221
     * @param string $type Endpoint type.
222
     */
223
    public function destroyEndpoint($type)
224
    {
225
        unset($this->endpoints[$type]);
226
    }
227
228
    /**
229
     * Adds query to the search.
230
     *
231
     * @param BuilderInterface $query
232
     * @param string           $boolType
233
     * @param string           $key
234
     *
235
     * @return $this
236
     */
237
    public function addQuery(BuilderInterface $query, $boolType = BoolQuery::MUST, $key = null)
238
    {
239
        $endpoint = $this->getEndpoint(QueryEndpoint::NAME);
240
        $endpoint->addToBool($query, $boolType, $key);
0 ignored issues
show
Documentation introduced by
$boolType is of type string, but the function expects a array|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...
Bug introduced by
It seems like $key defined by parameter $key on line 237 can also be of type string; however, ONGR\ElasticsearchDSL\Se...tInterface::addToBool() does only seem to accept array|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
241
242
        return $this;
243
    }
244
245
    /**
246
     * Returns endpoint instance.
247
     *
248
     * @param string $type Endpoint type.
249
     *
250
     * @return SearchEndpointInterface
251
     */
252
    private function getEndpoint($type)
253
    {
254
        if (!array_key_exists($type, $this->endpoints)) {
255
            $this->endpoints[$type] = SearchEndpointFactory::get($type);
256
        }
257
258
        return $this->endpoints[$type];
259
    }
260
261
    /**
262
     * Returns queries inside BoolQuery instance.
263
     *
264
     * @return BoolQuery
265
     */
266
    public function getQueries()
267
    {
268
        $endpoint = $this->getEndpoint(QueryEndpoint::NAME);
269
270
        return $endpoint->getBool();
271
    }
272
273
    /**
274
     * Sets query endpoint parameters.
275
     *
276
     * @param array $parameters
277
     *
278
     * @return $this
279
     */
280
    public function setQueryParameters(array $parameters)
281
    {
282
        $this->setEndpointParameters(QueryEndpoint::NAME, $parameters);
283
284
        return $this;
285
    }
286
287
    /**
288
     * Sets parameters to the endpoint.
289
     *
290
     * @param string $endpointName
291
     * @param array  $parameters
292
     *
293
     * @return $this
294
     */
295
    public function setEndpointParameters($endpointName, array $parameters)
296
    {
297
        /** @var AbstractSearchEndpoint $endpoint */
298
        $endpoint = $this->getEndpoint($endpointName);
299
        $endpoint->setParameters($parameters);
300
301
        return $this;
302
    }
303
304
    /**
305
     * Adds a post filter to search.
306
     *
307
     * @param BuilderInterface $filter   Filter.
308
     * @param string           $boolType Example boolType values:
309
     *                                   - must
310
     *                                   - must_not
311
     *                                   - should.
312
     * @param string           $key
313
     *
314
     * @return $this.
0 ignored issues
show
Documentation introduced by
The doc-type $this. could not be parsed: Unknown type name "$this." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
315
     */
316
    public function addPostFilter(BuilderInterface $filter, $boolType = BoolQuery::MUST, $key = null)
317
    {
318
        $this
319
            ->getEndpoint(PostFilterEndpoint::NAME)
320
            ->addToBool($filter, $boolType, $key);
0 ignored issues
show
Documentation introduced by
$boolType is of type string, but the function expects a array|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...
Bug introduced by
It seems like $key defined by parameter $key on line 316 can also be of type string; however, ONGR\ElasticsearchDSL\Se...tInterface::addToBool() does only seem to accept array|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
321
322
        return $this;
323
    }
324
325
    /**
326
     * Returns queries inside BoolFilter instance.
327
     *
328
     * @return BoolQuery
329
     */
330
    public function getPostFilters()
331
    {
332
        $endpoint = $this->getEndpoint(PostFilterEndpoint::NAME);
333
334
        return $endpoint->getBool();
335
    }
336
337
    /**
338
     * Sets post filter endpoint parameters.
339
     *
340
     * @param array $parameters
341
     *
342
     * @return $this
343
     */
344
    public function setPostFilterParameters(array $parameters)
345
    {
346
        $this->setEndpointParameters(PostFilterEndpoint::NAME, $parameters);
347
348
        return $this;
349
    }
350
351
    /**
352
     * Adds aggregation into search.
353
     *
354
     * @param AbstractAggregation $aggregation
355
     *
356
     * @return $this
357
     */
358
    public function addAggregation(AbstractAggregation $aggregation)
359
    {
360
        $this->getEndpoint(AggregationsEndpoint::NAME)->add($aggregation, $aggregation->getName());
361
362
        return $this;
363
    }
364
365
    /**
366
     * Returns all aggregations.
367
     *
368
     * @return BuilderInterface[]
369
     */
370
    public function getAggregations()
371
    {
372
        return $this->getEndpoint(AggregationsEndpoint::NAME)->getAll();
373
    }
374
375
    /**
376
     * Adds inner hit into search.
377
     *
378
     * @param NestedInnerHit $innerHit
379
     *
380
     * @return $this
381
     */
382
    public function addInnerHit(NestedInnerHit $innerHit)
383
    {
384
        $this->getEndpoint(InnerHitsEndpoint::NAME)->add($innerHit, $innerHit->getName());
385
386
        return $this;
387
    }
388
389
    /**
390
     * Returns all inner hits.
391
     *
392
     * @return BuilderInterface[]
393
     */
394
    public function getInnerHits()
395
    {
396
        return $this->getEndpoint(InnerHitsEndpoint::NAME)->getAll();
397
    }
398
399
    /**
400
     * Adds sort to search.
401
     *
402
     * @param BuilderInterface $sort
403
     *
404
     * @return $this
405
     */
406
    public function addSort(BuilderInterface $sort)
407
    {
408
        $this->getEndpoint(SortEndpoint::NAME)->add($sort);
409
410
        return $this;
411
    }
412
413
    /**
414
     * Returns all set sorts.
415
     *
416
     * @return BuilderInterface[]
417
     */
418
    public function getSorts()
419
    {
420
        return $this->getEndpoint(SortEndpoint::NAME)->getAll();
421
    }
422
423
    /**
424
     * Allows to highlight search results on one or more fields.
425
     *
426
     * @param Highlight $highlight
427
     *
428
     * @return $this.
0 ignored issues
show
Documentation introduced by
The doc-type $this. could not be parsed: Unknown type name "$this." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
429
     */
430
    public function addHighlight($highlight)
431
    {
432
        $this->getEndpoint(HighlightEndpoint::NAME)->add($highlight);
433
434
        return $this;
435
    }
436
437
    /**
438
     * Returns highlight builder.
439
     *
440
     * @return BuilderInterface
441
     */
442
    public function getHighlights()
443
    {
444
        /** @var HighlightEndpoint $highlightEndpoint */
445
        $highlightEndpoint = $this->getEndpoint(HighlightEndpoint::NAME);
446
447
        return $highlightEndpoint->getHighlight();
448
    }
449
450
    /**
451
     * Adds suggest into search.
452
     *
453
     * @param BuilderInterface $suggest
454
     *
455
     * @return $this
456
     */
457
    public function addSuggest(NamedBuilderInterface $suggest)
458
    {
459
        $this->getEndpoint(SuggestEndpoint::NAME)->add($suggest, $suggest->getName());
0 ignored issues
show
Documentation introduced by
$suggest->getName() is of type string, but the function expects a array|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...
460
461
        return $this;
462
    }
463
464
    /**
465
     * Returns all suggests.
466
     *
467
     * @return BuilderInterface[]
468
     */
469
    public function getSuggests()
470
    {
471
        return $this->getEndpoint(SuggestEndpoint::NAME)->getAll();
472
    }
473
474
    /**
475
     * @return int
476
     */
477
    public function getFrom()
478
    {
479
        return $this->from;
480
    }
481
482
    /**
483
     * @param int $from
484
     *
485
     * @return $this
486
     */
487
    public function setFrom($from)
488
    {
489
        $this->from = $from;
490
491
        return $this;
492
    }
493
494
    /**
495
     * @param int $time
496
     *
497
     * @return $this
498
     */
499
    public function setTerminateAfter(int $time)
500
    {
501
        $this->terminateAfter = $time;
502
503
        return $this;
504
    }
505
506
    /**
507
     * @param string $time
508
     *
509
     * @return $this
510
     */
511
    public function setTimeout($time)
512
    {
513
        $this->timeout = $time;
0 ignored issues
show
Documentation Bug introduced by
It seems like $time of type string is incompatible with the declared type object<ONGR\ElasticsearchDSL\time> of property $timeout.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
514
515
        return $this;
516
    }
517
518
    /**
519
     * @return bool
520
     */
521
    public function isTrackTotalHits()
522
    {
523
        return $this->trackTotalHits;
524
    }
525
526
    /**
527
     * @param bool $trackTotalHits
528
     *
529
     * @return $this
530
     */
531
    public function setTrackTotalHits(bool $trackTotalHits)
532
    {
533
        $this->trackTotalHits = $trackTotalHits;
534
535
        return $this;
536
    }
537
538
    /**
539
     * @return int
540
     */
541
    public function getSize()
542
    {
543
        return $this->size;
544
    }
545
546
    /**
547
     * @param int $size
548
     *
549
     * @return $this
550
     */
551
    public function setSize($size)
552
    {
553
        $this->size = $size;
554
555
        return $this;
556
    }
557
558
    /**
559
     * @return bool
560
     */
561
    public function isSource()
562
    {
563
        return $this->source;
564
    }
565
566
    /**
567
     * @param bool $source
568
     *
569
     * @return $this
570
     */
571
    public function setSource($source)
572
    {
573
        $this->source = $source;
574
575
        return $this;
576
    }
577
578
    /**
579
     * @return array
580
     */
581
    public function getStoredFields()
582
    {
583
        return $this->storedFields;
584
    }
585
586
    /**
587
     * @param array $storedFields
588
     *
589
     * @return $this
590
     */
591
    public function setStoredFields($storedFields)
592
    {
593
        $this->storedFields = $storedFields;
594
595
        return $this;
596
    }
597
598
    /**
599
     * @return array
600
     */
601
    public function getScriptFields()
602
    {
603
        return $this->scriptFields;
604
    }
605
606
    /**
607
     * @param array $scriptFields
608
     *
609
     * @return $this
610
     */
611
    public function setScriptFields($scriptFields)
612
    {
613
        $this->scriptFields = $scriptFields;
614
615
        return $this;
616
    }
617
618
    /**
619
     * @return array
620
     */
621
    public function getDocValueFields()
622
    {
623
        return $this->docValueFields;
624
    }
625
626
    /**
627
     * @param array $docValueFields
628
     *
629
     * @return $this
630
     */
631
    public function setDocValueFields($docValueFields)
632
    {
633
        $this->docValueFields = $docValueFields;
634
635
        return $this;
636
    }
637
638
    /**
639
     * @return bool
640
     */
641
    public function isExplain()
642
    {
643
        return $this->explain;
644
    }
645
646
    /**
647
     * @param bool $explain
648
     *
649
     * @return $this
650
     */
651
    public function setExplain($explain)
652
    {
653
        $this->explain = $explain;
654
655
        return $this;
656
    }
657
658
    /**
659
     * @return bool
660
     */
661
    public function isVersion()
662
    {
663
        return $this->version;
664
    }
665
666
    /**
667
     * @param bool $version
668
     *
669
     * @return $this
670
     */
671
    public function setVersion($version)
672
    {
673
        $this->version = $version;
674
675
        return $this;
676
    }
677
678
    /**
679
     * @return array
680
     */
681
    public function getIndicesBoost()
682
    {
683
        return $this->indicesBoost;
684
    }
685
686
    /**
687
     * @param array $indicesBoost
688
     *
689
     * @return $this
690
     */
691
    public function setIndicesBoost($indicesBoost)
692
    {
693
        $this->indicesBoost = $indicesBoost;
694
695
        return $this;
696
    }
697
698
    /**
699
     * @return int
700
     */
701
    public function getMinScore()
702
    {
703
        return $this->minScore;
704
    }
705
706
    /**
707
     * @param int $minScore
708
     *
709
     * @return $this
710
     */
711
    public function setMinScore($minScore)
712
    {
713
        $this->minScore = $minScore;
714
715
        return $this;
716
    }
717
718
    /**
719
     * @return array
720
     */
721
    public function getSearchAfter()
722
    {
723
        return $this->searchAfter;
724
    }
725
726
    /**
727
     * @param array $searchAfter
728
     *
729
     * @return $this
730
     */
731
    public function setSearchAfter($searchAfter)
732
    {
733
        $this->searchAfter = $searchAfter;
734
735
        return $this;
736
    }
737
738
    /**
739
     * @return string
740
     */
741
    public function getScroll()
742
    {
743
        return $this->scroll;
744
    }
745
746
    /**
747
     * @param string $scroll
748
     *
749
     * @return $this
750
     */
751
    public function setScroll($scroll = '5m')
752
    {
753
        $this->scroll = $scroll;
754
755
        $this->addUriParam('scroll', $this->scroll);
756
757
        return $this;
758
    }
759
760
    /**
761
     * @param string $name
762
     * @param string|array|bool|int $value
763
     *
764
     * @return $this
765
     */
766
    public function addUriParam($name, $value)
767
    {
768
        if (in_array($name, [
769
            'q',
770
            'df',
771
            'analyzer',
772
            'analyze_wildcard',
773
            'default_operator',
774
            'lenient',
775
            'explain',
776
            '_source',
777
            '_source_exclude',
778
            '_source_include',
779
            'stored_fields',
780
            'sort',
781
            'track_scores',
782
            'timeout',
783
            'terminate_after',
784
            'from',
785
            'size',
786
            'search_type',
787
            'scroll',
788
            'allow_no_indices',
789
            'ignore_unavailable',
790
            'typed_keys',
791
            'pre_filter_shard_size',
792
            'ignore_unavailable',
793
        ])) {
794
            $this->uriParams[$name] = $value;
795
        } else {
796
            throw new \InvalidArgumentException(sprintf('Parameter %s is not supported.', $value));
797
        }
798
799
        return $this;
800
    }
801
802
    /**
803
     * Returns query url parameters.
804
     *
805
     * @return array
806
     */
807
    public function getUriParams()
808
    {
809
        return $this->uriParams;
810
    }
811
812
    /**
813
     * {@inheritdoc}
814
     */
815
    public function toArray()
816
    {
817
        $output = array_filter(static::$serializer->normalize($this->endpoints));
0 ignored issues
show
Bug introduced by
Since $serializer is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $serializer to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
818
819
        $params = [
820
            'from' => 'from',
821
            'size' => 'size',
822
            'timeout' => 'timeout',
823
            'source' => '_source',
824
            'storedFields' => 'stored_fields',
825
            'scriptFields' => 'script_fields',
826
            'docValueFields' => 'docvalue_fields',
827
            'explain' => 'explain',
828
            'version' => 'version',
829
            'indicesBoost' => 'indices_boost',
830
            'minScore' => 'min_score',
831
            'searchAfter' => 'search_after',
832
            'trackTotalHits' => 'track_total_hits',
833
            'terminateAfter' => 'terminate_after'
834
        ];
835
836
        foreach ($params as $field => $param) {
837
            if ($this->$field !== null) {
838
                $output[$param] = $this->$field;
839
            }
840
        }
841
842
        return $output;
843
    }
844
}
845