-
Notifications
You must be signed in to change notification settings - Fork 735
/
Query.php
472 lines (427 loc) · 15 KB
/
Query.php
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
<?php
namespace Elastica;
use Elastica\Aggregation\AbstractAggregation;
use Elastica\Exception\InvalidException;
use Elastica\Query\AbstractQuery;
use Elastica\Query\MatchAll;
use Elastica\Query\QueryString;
use Elastica\Rescore\Query as QueryRescore;
use Elastica\Script\AbstractScript;
use Elastica\Script\ScriptFields;
use Elastica\Suggest\AbstractSuggest;
/**
* Elastica query object.
*
* Creates different types of queries
*
* @author Nicolas Ruflin <[email protected]>
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html
* @todo: improve THighlightArgs https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html
* @phpstan-type THighlightArgs = array<mixed>
* @phpstan-type TStoredFields = list<string>
* @todo: improve TDocValueFields https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-docvalue-fields
* @phpstan-type TDocValueFields = array<mixed>
* @phpstan-type TRescoreArgs = QueryRescore|list<QueryRescore>
* @phpstan-type TSourceArgs = non-empty-string|list<non-empty-string>|array{includes?: list<non-empty-string>, excludes?: list<non-empty-string>}|false
* @phpstan-type TSortArrayArg = array<string, string>|array<string, array{
* order?: non-empty-string,
* mode?: non-empty-string,
* numeric_type?: non-empty-string,
* nested?: array{path: non-empty-string, filter?: array<mixed>, max_children?: int, nested?: array<mixed>},
* missing?: non-empty-string,
* unmapped_type?: non-empty-string,
* }>|array{_geo_distance: array<string, mixed>}
* @phpstan-type TSortArg = non-empty-string|TSortArrayArg
* @phpstan-type TSortArgs = list<TSortArg>|TSortArrayArg
* @phpstan-type TRawQuery = array{
* _source?: TSourceArgs,
* aggs?: list<AbstractAggregation>|array<string, array<string, array<string, mixed>>>,
* collapse?: Collapse,
* docvalue_fields?: TDocValueFields,
* explain?: bool,
* from?: int,
* highlight?: THighlightArgs,
* indices_boost?: array<string, float>,
* min_score?: float,
* pit?: PointInTime,
* post_filter?: AbstractQuery,
* query?: AbstractQuery|array<string, array<string, mixed>>,
* rescore?: TRescoreArgs,
* script_fields?: ScriptFields,
* size?: int,
* sort?: TSortArgs,
* stored_fields?: TStoredFields,
* suggest?: Suggest,
* track_scores?: bool,
* track_total_hits?: bool|int,
* version?: bool,
* }
* @phpstan-type TCreateQueryArgsMatching = AbstractQuery|TRawQuery|self|string|null
* @phpstan-type TCreateQueryArgs = TCreateQueryArgsMatching|AbstractSuggest|Collapse|Suggest
*/
class Query extends Param
{
/**
* If the current query has a suggest in it.
*
* @var bool
*/
private $hasSuggest = false;
/**
* Creates a query object.
*
* @param AbstractQuery|array|Collapse|Suggest $query Query object (default = null)
* @phpstan-param AbstractQuery|Suggest|Collapse|TRawQuery $query
*/
public function __construct($query = null)
{
if (\is_array($query)) {
$this->setRawQuery($query);
} elseif ($query instanceof AbstractQuery) {
$this->setQuery($query);
} elseif ($query instanceof Suggest) {
$this->setSuggest($query);
} elseif ($query instanceof Collapse) {
$this->setCollapse($query);
}
}
/**
* Transforms the argument to a query object.
*
* For example, an empty argument will return a \Elastica\Query with a \Elastica\Query\MatchAll.
*
* @param AbstractQuery|AbstractSuggest|array|Collapse|Query|string|Suggest|null $query
* @phpstan-param TCreateQueryArgs $query
*
* @throws InvalidException For an invalid argument
*/
public static function create($query): self
{
switch (true) {
case empty($query):
return new static(new MatchAll());
case $query instanceof self:
return $query;
case $query instanceof AbstractSuggest:
return new static(new Suggest($query));
case $query instanceof AbstractQuery:
case $query instanceof Suggest:
case $query instanceof Collapse:
case \is_array($query):
return new static($query);
case \is_string($query):
return new static(new QueryString($query));
}
throw new InvalidException('Unexpected argument to create a query for.');
}
/**
* Sets query as raw array. Will overwrite all already set arguments.
*
* @param array $query Query array
* @phpstan-param TRawQuery $query
*/
public function setRawQuery(array $query): self
{
$this->_params = $query;
return $this;
}
public function setQuery(AbstractQuery $query): self
{
return $this->setParam('query', $query);
}
/**
* Gets the query object.
*
* @return AbstractQuery|array<string, array<string, mixed>>
*/
public function getQuery()
{
return $this->getParam('query');
}
/**
* Sets the start from which the search results should be returned.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-from-size
*/
public function setFrom(int $from): self
{
return $this->setParam('from', $from);
}
/**
* Sets sort arguments for the query
* Replaces existing values.
*
* @param array $sortArgs Sorting arguments
* @phpstan-param TSortArgs $sortArgs
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html
*/
public function setSort(array $sortArgs): self
{
return $this->setParam('sort', $sortArgs);
}
/**
* Adds a sort param to the query.
*
* @param mixed $sort Sort parameter
* @phpstan-param TSortArg $sort
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html
*/
public function addSort($sort): self
{
return $this->addParam('sort', $sort);
}
/**
* Keep track of the scores when sorting results.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html#_track_scores
*/
public function setTrackScores(bool $trackScores = true): self
{
return $this->setParam('track_scores', $trackScores);
}
/**
* Sets highlight arguments for the query.
*
* @param array $highlightArgs Set all highlight arguments
* @phpstan-param THighlightArgs $highlightArgs
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html
*/
public function setHighlight(array $highlightArgs): self
{
return $this->setParam('highlight', $highlightArgs);
}
/**
* Adds a highlight argument.
*
* @param mixed $highlight Add highlight argument
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html
*/
public function addHighlight($highlight): self
{
return $this->addParam('highlight', $highlight);
}
/**
* Sets maximum number of results for this query.
*
* @param int $size Maximal number of results for query (default = 10)
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-from-size
*/
public function setSize(int $size = 10): self
{
return $this->setParam('size', $size);
}
/**
* Enables explain on the query.
*
* @param bool $explain Enabled or disable explain (default = true)
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-explain
*/
public function setExplain($explain = true): self
{
return $this->setParam('explain', $explain);
}
/**
* Enables version on the query.
*
* @param bool $version Enabled or disable version (default = true)
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-version
*/
public function setVersion($version = true): self
{
return $this->setParam('version', $version);
}
/**
* Sets the fields to be returned by the search
* NOTICE php will encode modified(or named keys) array into object format in json format request
* so the fields array must a sequence(list) type of array.
*
* @param array $fields Fields to be returned
* @phpstan-param TStoredFields $fields
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-stored-fields
*/
public function setStoredFields(array $fields): self
{
return $this->setParam('stored_fields', $fields);
}
/**
* Set the doc value representation of a fields to return for each hit.
*
* @param array $fieldDataFields Fields not stored to be returned
* @phpstan-param TDocValueFields $fieldDataFields
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-docvalue-fields
*/
public function setFieldDataFields(array $fieldDataFields): self
{
return $this->setParam('docvalue_fields', $fieldDataFields);
}
/**
* Set script fields.
*
* @param array<string, AbstractScript>|ScriptFields $scriptFields Script fields
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-script-fields
*/
public function setScriptFields($scriptFields): self
{
if (\is_array($scriptFields)) {
$scriptFields = new ScriptFields($scriptFields);
}
return $this->setParam('script_fields', $scriptFields);
}
/**
* Adds a Script to the query.
*/
public function addScriptField(string $name, AbstractScript $script): self
{
if (isset($this->_params['script_fields'])) {
$this->_params['script_fields']->addScript($name, $script);
} else {
$this->setScriptFields([$name => $script]);
}
return $this;
}
/**
* Adds an Aggregation to the query.
*/
public function addAggregation(AbstractAggregation $agg): self
{
$this->_params['aggs'][] = $agg;
return $this;
}
/**
* Converts all query params to an array.
*/
public function toArray(): array
{
if (!$this->hasSuggest && !isset($this->_params['query'])) {
$this->setQuery(new MatchAll());
}
if (isset($this->_params['post_filter']) && 0 === \count($this->_params['post_filter'])) {
unset($this->_params['post_filter']);
}
$array = $this->_convertArrayable($this->_params);
if (isset($array['suggest'])) {
$array['suggest'] = $array['suggest']['suggest'];
}
return $array;
}
/**
* Allows filtering of documents based on a minimum score.
*
* @param float $minScore Minimum score to filter documents by
*
* @throws InvalidException
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-min-score
*/
public function setMinScore(float $minScore): self
{
return $this->setParam('min_score', $minScore);
}
/**
* Add a suggest term.
*
* @param Suggest $suggest suggestion object
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters.html
*/
public function setSuggest(Suggest $suggest): self
{
$this->setParam('suggest', $suggest);
$this->hasSuggest = true;
return $this;
}
/**
* Add a Rescore.
*
* @param mixed $rescore suggestion object
* @phpstan-param TRescoreArgs $rescore
*
* @see: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-rescore
*/
public function setRescore($rescore): self
{
if (\is_array($rescore)) {
$buffer = [];
foreach ($rescore as $rescoreQuery) {
$buffer[] = $rescoreQuery;
}
} else {
$buffer = $rescore;
}
return $this->setParam('rescore', $buffer);
}
/**
* Sets the _source field to be returned with every hit.
*
* @param array|bool $params Fields to be returned or false to disable source
* @phpstan-param TSourceArgs $params
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-source-filtering
*/
public function setSource($params): self
{
return $this->setParam('_source', $params);
}
/**
* Sets a post_filter to the current query.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-post-filter
*/
public function setPostFilter(AbstractQuery $filter): self
{
return $this->setParam('post_filter', $filter);
}
/**
* Allows to collapse search results based on field values.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-collapse
*/
public function setCollapse(Collapse $collapse): self
{
return $this->setParam('collapse', $collapse);
}
/**
* Set the Point-in-Time used for the query.
* Use for results pagination with Search with search_after requests.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/paginate-search-results.html#search-after
*/
public function setPointInTime(PointInTime $pit): self
{
return $this->setParam('pit', $pit);
}
/**
* @param array<string, float> $indicesBoost
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-multiple-indices.html#index-boost
*/
public function setIndicesBoost(array $indicesBoost): self
{
return $this->setParam('indices_boost', \array_chunk($indicesBoost, 1, true));
}
/**
* Adds a track_total_hits argument.
*
* @param bool|int $trackTotalHits Track total hits parameter
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-track-total-hits
*/
public function setTrackTotalHits($trackTotalHits = true): self
{
if (!\is_bool($trackTotalHits) && !\is_int($trackTotalHits)) {
throw new InvalidException('TrackTotalHits must be either a boolean, or an integer value');
}
return $this->setParam('track_total_hits', $trackTotalHits);
}
}