-
Notifications
You must be signed in to change notification settings - Fork 730
Expand file tree
/
Copy pathQueryTest.php
More file actions
664 lines (524 loc) · 18.8 KB
/
QueryTest.php
File metadata and controls
664 lines (524 loc) · 18.8 KB
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
<?php
declare(strict_types=1);
namespace Elastica\Test;
use Elastica\Aggregation\Terms as TermsAggregation;
use Elastica\Collapse;
use Elastica\Collapse\InnerHits;
use Elastica\Document;
use Elastica\Exception\InvalidException;
use Elastica\Mapping;
use Elastica\PointInTime;
use Elastica\Query;
use Elastica\Query\Term;
use Elastica\Query\Terms;
use Elastica\Rescore\Query as RescoreQuery;
use Elastica\Script\Script;
use Elastica\Script\ScriptFields;
use Elastica\Search;
use Elastica\Suggest;
use Elastica\Test\Base as BaseTest;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
/**
* @internal
*/
class QueryTest extends BaseTest
{
#[Group('unit')]
public function testRawQuery(): void
{
$textQuery = new Term(['title' => 'test']);
$query1 = Query::create($textQuery);
$query2 = new Query();
$query2->setRawQuery(['query' => ['term' => ['title' => 'test']]]);
$this->assertEquals($query1->toArray(), $query2->toArray());
}
#[Group('unit')]
public function testSuggestShouldNotRemoveOtherParameters(): void
{
$query1 = new Query();
$query2 = new Query();
$suggest = new Suggest();
$suggest->setGlobalText('test');
$query1->setSize(40);
$query1->setSuggest($suggest);
$query2->setSuggest($suggest);
$query2->setSize(40);
$this->assertEquals($query1->toArray(), $query2->toArray());
}
#[Group('unit')]
public function testArrayQuery(): void
{
$query = [
'query' => [
'text' => [
'title' => 'test',
],
],
];
$query1 = Query::create($query);
$query2 = new Query();
$query2->setRawQuery(['query' => ['text' => ['title' => 'test']]]);
$this->assertEquals($query1->toArray(), $query2->toArray());
}
#[Group('functional')]
public function testSetSort(): void
{
$index = $this->_createIndex();
$index->setMapping(new Mapping([
'firstname' => ['type' => 'text', 'fielddata' => true],
'lastname' => ['type' => 'text', 'fielddata' => true],
]));
$index->addDocuments([
new Document('1', ['name' => 'hello world']),
new Document('2', ['firstname' => 'guschti', 'lastname' => 'ruflin']),
new Document('3', ['firstname' => 'nicolas', 'lastname' => 'ruflin']),
]);
$index->refresh();
$queryTerm = new Term();
$queryTerm->setTerm('lastname', 'ruflin');
$query = Query::create($queryTerm);
// ASC order
$query->setSort([['firstname' => ['order' => 'asc']]]);
$resultSet = $index->search($query);
$this->assertEquals(2, $resultSet->count());
$first = $resultSet->current()->getData();
$resultSet->next();
$second = $resultSet->current()->getData();
$this->assertEquals('guschti', $first['firstname']);
$this->assertEquals('nicolas', $second['firstname']);
// DESC order
$query->setSort(['firstname' => ['order' => 'desc']]);
$resultSet = $index->search($query);
$this->assertEquals(2, $resultSet->count());
$first = $resultSet->current()->getData();
$resultSet->next();
$second = $resultSet->current()->getData();
$this->assertEquals('nicolas', $first['firstname']);
$this->assertEquals('guschti', $second['firstname']);
}
#[Group('unit')]
public function testAddSort(): void
{
$query = new Query();
$sortParam = ['firstname' => ['order' => 'asc']];
$query->addSort($sortParam);
$this->assertEquals($query->getParam('sort'), [$sortParam]);
}
#[Group('unit')]
public function testSetTrackScores(): void
{
$query = new Query();
$param = false;
$query->setTrackScores($param);
$this->assertEquals($param, $query->getParam('track_scores'));
}
#[Group('unit')]
public function testSetRawQuery(): void
{
$query = new Query();
$params = ['query' => ['term' => ['title' => 'test']]];
$query->setRawQuery($params);
$this->assertEquals($params, $query->toArray());
}
#[Group('unit')]
public function testSetStoredFields(): void
{
$query = (new Query())
->setStoredFields(['firstname', 'lastname'])
;
$data = $query->toArray();
$this->assertArrayHasKey('stored_fields', $data);
$this->assertContains('firstname', $data['stored_fields']);
$this->assertContains('lastname', $data['stored_fields']);
$this->assertCount(2, $data['stored_fields']);
}
#[Group('unit')]
public function testGetQuery(): void
{
$query = new Query();
try {
$query->getQuery();
$this->fail('should throw exception because query does not exist');
} catch (InvalidException $e) {
$this->assertTrue(true);
}
$termQuery = new Term();
$termQuery->setTerm('text', 'value');
$query->setQuery($termQuery);
$this->assertSame($termQuery, $query->getQuery());
}
#[Group('unit')]
public function testSetQueryToArrayCast(): void
{
$query = new Query();
$termQuery = new Term();
$termQuery->setTerm('text', 'value');
$query->setQuery($termQuery);
$termQuery->setTerm('text', 'another value');
$anotherQuery = new Query();
$anotherQuery->setQuery($termQuery);
$this->assertEquals($query->toArray(), $anotherQuery->toArray());
}
#[Group('unit')]
public function testNotCloneInnerObjects(): void
{
$query = new Query();
$termQuery = new Term();
$termQuery->setTerm('text', 'value');
$query->setQuery($termQuery);
$anotherQuery = clone $query;
$termQuery->setTerm('text', 'another value');
$this->assertEquals($query->toArray(), $anotherQuery->toArray());
}
#[Group('unit')]
public function testSetQueryToArrayChangeQuery(): void
{
$query = new Query();
$termQuery = new Term();
$termQuery->setTerm('text', 'value');
$query->setQuery($termQuery);
$queryArray = $query->toArray();
$termQuery = $query->getQuery();
$this->assertInstanceOf(Term::class, $termQuery);
$termQuery->setTerm('text', 'another value');
$this->assertNotEquals($queryArray, $query->toArray());
}
#[Group('unit')]
public function testSetScriptFieldsToArrayCast(): void
{
$query = new Query();
$scriptFields = new ScriptFields();
$scriptFields->addScript('script', new Script('script'));
$query->setScriptFields($scriptFields);
$scriptFields->addScript('another script', new Script('another script'));
$anotherQuery = new Query();
$anotherQuery->setScriptFields($scriptFields);
$this->assertEquals($query->toArray(), $anotherQuery->toArray());
}
#[Group('unit')]
public function testAddScriptFieldsToArrayCast(): void
{
$query = new Query();
$scriptField = new Script('script');
$query->addScriptField('script', $scriptField);
$scriptField->setScript('another script');
$anotherQuery = new Query();
$anotherQuery->addScriptField('script', $scriptField);
$this->assertEquals($query->toArray(), $anotherQuery->toArray());
}
#[Group('unit')]
public function testAddScriptFieldToExistingScriptFields(): void
{
$script1 = new Script('s1');
$script2 = new Script('s2');
// add script1, then add script2
$query = new Query();
$scriptFields1 = new ScriptFields();
$scriptFields1->addScript('script1', $script1);
$query->setScriptFields($scriptFields1);
$query->addScriptField('script2', $script2);
// add script1 and script2 at once
$anotherQuery = new Query();
$scriptFields2 = new ScriptFields();
$scriptFields2->addScript('script1', $script1);
$scriptFields2->addScript('script2', $script2);
$anotherQuery->setScriptFields($scriptFields2);
$this->assertEquals($query->toArray(), $anotherQuery->toArray());
}
#[Group('unit')]
public function testAddAggregationToArrayCast(): void
{
$query = new Query();
$aggregation = new TermsAggregation('text');
$aggregation->setField('field');
$query->addAggregation($aggregation);
$aggregation->setName('another text');
$anotherQuery = new Query();
$anotherQuery->addAggregation($aggregation);
$this->assertEquals($query->toArray(), $anotherQuery->toArray());
}
#[Group('unit')]
public function testSetSuggestToArrayCast(): void
{
$query = new Query();
$suggest = new Suggest();
$suggest->setGlobalText('text');
$query->setSuggest($suggest);
$suggest->setGlobalText('another text');
$anotherQuery = new Query();
$anotherQuery->setSuggest($suggest);
$this->assertEquals($query->toArray(), $anotherQuery->toArray());
}
#[Group('unit')]
public function testSetRescoreToArrayCast(): void
{
$query = new Query();
$rescore = new RescoreQuery();
$rescore->setQueryWeight(1);
$query->setRescore($rescore);
$rescore->setQueryWeight(2);
$anotherQuery = new Query();
$anotherQuery->setRescore($rescore);
$this->assertEquals($query->toArray(), $anotherQuery->toArray());
}
#[Group('unit')]
public function testSetPostFilterToArrayCast(): void
{
$query = new Query();
$postFilter = new Terms('key', ['term']);
$query->setPostFilter($postFilter);
$postFilter->addTerm('another term');
$anotherQuery = new Query();
$anotherQuery->setPostFilter($postFilter);
$this->assertEquals($query->toArray(), $anotherQuery->toArray());
}
#[Group('unit')]
public function testRawPostFilter(): void
{
$query = (new Query())
->setQuery(new Term(['field' => 'value']))
->setParam('post_filter', [
'term' => ['field' => 'value'],
])
;
$this->assertSame([
'query' => [
'term' => [
'field' => 'value',
],
],
'post_filter' => [
'term' => [
'field' => 'value',
],
],
], $query->toArray());
}
#[Group('functional')]
public function testNoSource(): void
{
$index = $this->_createIndex();
// Adds 1 document to the index
$doc1 = new Document(
'1',
['username' => 'ruflin', 'test' => ['2', '3', '5']]
);
$index->addDocument($doc1);
// To update index
$index->refresh();
$query = Query::create('ruflin');
$resultSet = $index->search($query);
// Disable source
$query->setSource(false);
$resultSetNoSource = $index->search($query);
$this->assertEquals(1, $resultSet->count());
$this->assertEquals(1, $resultSetNoSource->count());
// Tests if no source is in response except id
$result = $resultSetNoSource->current();
$this->assertEquals(1, $result->getId());
$this->assertEmpty($result->getData());
// Tests if source is in response except id
$result = $resultSet->current();
$this->assertEquals(1, $result->getId());
$this->assertNotEmpty($result->getData());
}
#[Group('unit')]
public function testSetCollapseToArrayCast(): void
{
$query = new Query();
$collapse = new Collapse();
$collapse->setFieldname('some_field');
$query->setCollapse($collapse);
$collapse->setFieldname('another_field');
$anotherQuery = new Query();
$anotherQuery->setCollapse($collapse);
$this->assertEquals($query->toArray(), $anotherQuery->toArray());
}
#[Group('unit')]
public function testCollapseArrayStructure(): void
{
$query = new Query();
$collapse = new Collapse();
$collapse
->setFieldname('user')
->setInnerHits(
(new InnerHits())
->setName('last_tweets')
->setSize(5)
->setSort(['date' => 'asc'])
)
->setMaxConcurrentGroupSearches(4)
;
$query->setCollapse($collapse);
$expected = [
'field' => 'user',
'inner_hits' => [
'name' => 'last_tweets',
'size' => 5,
'sort' => ['date' => 'asc'],
],
'max_concurrent_group_searches' => 4,
];
$actual = $query->toArray();
$this->assertArrayHasKey('collapse', $actual);
$this->assertEquals($expected, $actual['collapse']);
}
#[Group('unit')]
public function testCollapseSecondLevelArrayStructure(): void
{
$query = new Query();
$collapse = new Collapse();
$collapse
->setFieldname('user')
->setInnerHits(
(new InnerHits())
->setName('last_tweets')
->setSize(5)
->setSort(['date' => 'asc'])
->setCollapse(
(new Collapse())
->setFieldname('date')
)
)
->setMaxConcurrentGroupSearches(4)
;
$query->setCollapse($collapse);
$expected = [
'field' => 'user',
'inner_hits' => [
'name' => 'last_tweets',
'size' => 5,
'sort' => ['date' => 'asc'],
'collapse' => [
'field' => 'date',
],
],
'max_concurrent_group_searches' => 4,
];
$actual = $query->toArray();
$this->assertArrayHasKey('collapse', $actual);
$this->assertEquals($expected, $actual['collapse']);
}
#[Group('unit')]
public function testIndicesBoostQuery(): void
{
$query = (new Query(new Term(['field' => 'value'])))
->setIndicesBoost(['foo' => 1.0, 'bar' => 2])
;
$expected = [
'query' => [
'term' => [
'field' => 'value',
],
],
'indices_boost' => [
['foo' => 1.0],
['bar' => 2],
],
];
$this->assertSame($expected, $query->toArray());
}
#[Group('unit')]
public function testPointInTimeInQuery(): void
{
$query = new Query();
$query->setPointInTime(new PointInTime('id-hash', '5m'));
$queryAsArray = $query->toArray();
$this->assertArrayHasKey('pit', $queryAsArray);
$this->assertArrayHasKey('id', $queryAsArray['pit']);
$this->assertSame('id-hash', $queryAsArray['pit']['id']);
$this->assertArrayHasKey('keep_alive', $queryAsArray['pit']);
$this->assertSame('5m', $queryAsArray['pit']['keep_alive']);
}
#[Group('functional')]
public function testPointInTimeQueryExecution(): void
{
$index = $this->_createIndex();
$pitId = $index->openPointInTime('20s')->getData()['id'];
$query = (new Query())->setPointInTime(new PointInTime($pitId, '20s'));
$search = (new Search($index->getClient()))->setQuery($query);
$this->assertSame($pitId, $search->search()->getPointInTimeId());
}
#[Group('unit')]
public function testSetTrackTotalHitsIsInParams(): void
{
$query = new Query();
$query->setTrackTotalHits(false);
$this->assertFalse($query->getParam('track_total_hits'));
}
#[DataProvider('provideSetTrackTotalHitsInvalidValue')]
#[Group('functional')]
public function testSetTrackTotalHitsInvalidValue($value): void
{
$this->expectException(InvalidException::class);
(new Query())->setTrackTotalHits($value);
}
public static function provideSetTrackTotalHitsInvalidValue(): iterable
{
yield 'string' => ['string string'];
yield 'null' => [null];
yield 'object' => [new \stdClass()];
yield 'array' => [[]];
}
#[Group('functional')]
public function testSetTrackTotalHits(): void
{
$index = $this->_createIndex();
$index->setMapping(new Mapping([
'firstname' => ['type' => 'text', 'fielddata' => true],
]));
$documents = [];
for ($i = 0; $i < 50; ++$i) {
$documents[] = new Document((string) $i, ['firstname' => 'antoine '.$i]);
}
$index->addDocuments($documents);
$queryTerm = new Term();
$queryTerm->setTerm('firstname', 'antoine');
$index->refresh();
$query = Query::create($queryTerm);
$resultSet = $index->search($query);
$this->assertEquals(50, $resultSet->getTotalHits());
$this->assertEquals('eq', $resultSet->getTotalHitsRelation());
$query->setTrackTotalHits(false);
$resultSet = $index->search($query);
$this->assertEquals(0, $resultSet->getTotalHits());
$this->assertEquals('eq', $resultSet->getTotalHitsRelation());
$query->setTrackTotalHits(25);
$resultSet = $index->search($query);
$this->assertEquals(25, $resultSet->getTotalHits());
$this->assertEquals('gte', $resultSet->getTotalHitsRelation());
}
#[Group('functional')]
public function testSearchAfter(): void
{
$index = $this->_createIndex();
$index->setMapping(new Mapping([
'country_id' => ['type' => 'integer'],
'region_id' => ['type' => 'integer'],
]));
$index->addDocuments([
new Document('1', ['country_id' => 1, 'region_id' => 1]),
new Document('2', ['country_id' => 1, 'region_id' => 2]),
new Document('3', ['country_id' => 2, 'region_id' => 3]),
]);
$index->refresh();
$query = new Query();
$query->setSize(2);
$query->setSort([
'country_id' => 'desc',
'region_id' => 'asc',
]);
$firstPageResultSet = $index->search($query);
$documents = $firstPageResultSet->getDocuments();
$this->assertCount(2, $documents);
/** @var Document $lastDocument */
$lastDocument = array_pop($documents);
$lastDocument->getParam('sort');
$this->assertNotEmpty($lastDocument->getSort());
$query->setSearchAfter($lastDocument->getSort());
$secondPageResultSet = $index->search($query);
$documents = $secondPageResultSet->getDocuments();
$this->assertCount(1, $documents);
}
}