-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathQueryBuilder.php
973 lines (916 loc) · 34.7 KB
/
QueryBuilder.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
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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
<?php
/**
* Copyright (c) 2015, Muhannad Shelleh
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
namespace ItvisionSy\EsMapper;
/**
* Elasticsearch DSL query builder
*
* The elastic query builder class is an easy way to create complex elastic DSL
* queries. It has a fluid interface, as well a smart detection feature to make
* building a query an easy task for developers not familier with elastic DSL
* terms and rules.
*
* Example:
* QueryBuilder::make([$initialQuery]) //initiate a builder with base query
* ->where('name','Elastic') //term:{name:'Elastic'}
* ->where('version', ['1.6','2.0']) //terms:{version:['1.6','2.0']}
* ->where(['sdk','client'],'PHP') //or:[{term:{sdk:'PHP'}},{term:{...}}]
* ->where(['created','updated'],['yesterday','today']) //or:[{terms},{terms}]
* ->toArray(); //get the final query array
*
* It worths to know that the query builder depends on the bool query/filter:
* [
* 'query'=>[
* 'filtered'=>[
* 'filter'=>[
* 'bool'=>[
* 'must'=>[...],
* 'should'=>[...],
* 'must_not'=>[...],
* ]
* ],
* 'query'=>[
* 'bool'=>[
* 'must'=>[...],
* 'should'=>[...],
* 'must_not'=>[...],
* ]
* ]
* ]
* ]
* ]
*
* @package ItvisionSy\EsMapper
* @author Muhannad Shelleh <[email protected]>
*
* @method static|self|QueryBuilder make(array $baseQuery=[])
* @method-static static|self|QueryBuilder make(array $baseQuery=[])
*
* @see AutoQueryBuilder
*
*/
class QueryBuilder {
protected $query = [];
public static function __callStatic($name, $arguments) {
switch ($name) {
case 'make':
return static::__make(array_key_exists(0, $arguments) ? $arguments[0] : []);
}
}
public function __call($name, $arguments) {
$queryName = "{$name}Query";
if (method_exists($this, $queryName)) {
return call_user_func_array([$this, $queryName], $arguments);
}
}
protected static function __make(array $query = []) {
return new static($query);
}
/**
*
* @param array $query The base query to start building on it
*/
public function __construct(array $query = []) {
$this->query = array_merge_recursive($query, []);
}
/**
* Returns the final DSL query as a PHP assoc array
* @return array
*/
public function toArray() {
return $this->query;
}
/**
* Returns the final DSL query as a JSON string
* @return type
*/
public function toJSON() {
return json_encode($this->query);
}
/**
* Resets the query to an empty query.
*
* @return QueryBuilder|static|self
*/
public function emptyQuery(array $baseQuery = []) {
$this->query = $baseQuery;
return $this;
}
/**
* Adds a sort clause to the query
*
* @param string $by the key to sort by it
* @param string|array $direction the direction of the sort.
* Can be an array for extra sort control.
* @param boolean $override false by default. if true it will reset the sort
* first, then add the new sort entry.
* @return QueryBuilder|static|self
*/
public function sort($by, $direction = "asc", $override = false) {
$this->assertInitiated("sort");
if ($override) {
$this->query['sort'] = [];
}
$this->query['sort'][] = [$by => $direction];
return $this;
}
/**
* The basic smart method to build queries.
*
* It will automatically identify the correct filter/query tool to use, as
* well the correct query part to be used in, depending on the content and
* type of the key, value, and comparison parameters.
*
* $comparison parameter will define what filter/query tool to use.
*
* @param string|string[] $key the key(s) to check
* @param string|string[] $value the value(s) to check against
* @param string $compare the comparison operator or elastic query/filter tool
* prefix it with ! to negate or with ? to convert to should
* @param boolean $filter false to add as a query, true as a filter
* @param array|map $params additional parameters the query/filter can use
* @return static|QueryBuilder
*/
public function where($key, $value, $compare = "=", $filter = null, $params = []) {
//identify the bool part must|should|must_not
$compare = trim($compare);
$bool = substr($compare, 0, 1) == "!" ? "must_not" : (substr($compare, 0, 1) == "?" ? "should" : "must");
//get the correct compare value
if ($bool !== "must") {
$compare = substr($compare, 1);
}
//$suffix and $prefix will be used in regex, wildcard, prefix, and suffix
//queries.
$tool = $suffix = $prefix = null;
//$_filter is the real identifier for the filter
$_filter = $filter;
//identify the tool, operator, and part
switch (strtolower(str_replace("_", " ", $compare))) {
case '=':
case 'equals':
case 'term':
default:
$tool = "term";
break;
case '>':
case 'gt':
$tool = "range";
$operator = "gt";
break;
case '<':
case 'lt':
$tool = "range";
$operator = "lt";
break;
case '><':
case '<>':
case 'between':
$tool = "range";
$operator = ["gt", "lt"];
break;
case '>=<=':
case '<=>=':
case 'between from to':
$tool = "range";
$operator = ["gte", "lte"];
break;
case '>=<':
case '<>=':
case 'between from':
$tool = "range";
$operator = ["gte", "lt"];
break;
case '><=':
case '<=>':
case 'between to':
$tool = "range";
$operator = ["gt", "lte"];
break;
case '>=':
case 'gte':
$tool = "range";
$operator = "gte";
break;
case '<=':
case 'lte':
$tool = "range";
$operator = "lte";
break;
case '*=':
case 'suffix':
case 'suffixed':
case 'ends with':
case 'ends':
$tool = "wildcard";
$prefix = "*";
$_filter = false;
break;
case '=*':
case 'starts':
case 'starts with':
case 'prefix':
case 'prefixed':
$tool = "prefix";
break;
case '*=*':
case 'like':
case 'wildcard':
$tool = "wildcard";
$prefix = '*';
$suffix = '*';
$_filter = false;
break;
case '**':
case 'r':
case 'regexp':
case 'regex':
case 'rx':
$tool = "regexp";
break;
case '*':
case 'match':
$tool = "match";
$_filter = false;
break;
}
//add prefix/suffix to each element in array values
if ($suffix || $prefix) {
if (is_array($value)) {
foreach ($value as $index => $singleValue) {
if (is_string($singleValue)) {
$value[$index] = $prefix . $singleValue . $suffix;
}
}
} else {
$value = ($prefix? : "") . $value . ($suffix? : "");
}
}
//call the real query builder method
switch ($tool) {
case 'match':
return $this->match($key, $value, $bool, $this->shouldBeFilter($filter, $_filter), $params);
case 'regexp':
return $this->regexp($key, $value, $bool, $this->shouldBeFilter($filter, $_filter), $params);
case 'term':
return $this->term($key, $value, $bool, $this->shouldBeFilter($filter, $_filter), $params);
case 'range':
return $this->range($key, $operator, $value, $bool, $this->shouldBeFilter($filter, $_filter), $params);
case 'wildcard':
return $this->wildcard($key, $value, $bool, $this->shouldBeFilter($filter, $_filter), $params);
case 'prefix':
return $this->prefix($key, $value, $bool, $this->shouldBeFilter($filter, $_filter), $params);
}
}
/**
* Creates a wildcard query part.
*
* It will automatically detect the $key and $value types to create the
* required number of clauses, as follows:
*
* $key $value result
*
* single single {wildcard:{$key:{wildcard:$value}}}
* single array or:[wildcard($key, $value[1]), ...]
* array single or:[wildcard($key[1], $value), ...]
* array array or:[wildcard($key[1], $value[1]),
* wildcard($key[1], $value[2]), ...
* wildcard($key[2], $value[1]), ... ]
*
* If $filter is true, it will enclose the wildcard query with `query`
* filter and add it to the DSL query filter section instead the query
* section.
*
* @param string|string[] $key the key(s) to wildcard search in.
* @param string|string[] $value the value(s) to wildcard search against.
* @param string $bool severity of the query/filter. [must]|must_not|should
* @param bool $filter if true, DSL query filter section will be used after
* enclosing in a `query` filter.
* @param array $params extra parameters for the query tool
* @return QueryBuilder|static|self
*/
public function wildcard($key, $value, $bool = "must", $filter = false, array $params = []) {
if (is_array($key)) {
return $this->multiWildcard($key, $value, $bool, $filter, $params);
}
if (is_array($value)) {
return $this->wildcards($key, $value, $bool, $filter, $params);
}
$this->addBool($this->makeFilteredQuery(["wildcard" => [$key => ["wildcard" => $value]]], $filter), $bool, $filter, $params);
return $this;
}
/**
* Creates wildcard query for each $value grouped by OR clause
*
* @see QueryBuilder::wildcard() for more information
*
* @param string|string[] $key the key(s) to wildcard search in.
* @param string[] $values the value(s) to wildcard search against.
* @param string $bool severity of the query/filter. [must]|must_not|should
* @param bool $filter if true, DSL query filter section will be used after
* enclosing in a `query` filter.
* @param array $params extra parameters for the query tool
* @return QueryBuilder|static|self
*/
public function wildcards($key, array $values, $bool = "must", $filter = false, array $params = []) {
$subQuery = $this->orWhere($bool);
foreach ($values as $value) {
$subQuery->wildcard($key, $value, $bool, true, $params);
}
$subQuery->endSubQuery();
return $this;
}
/**
* Creates wildcard query for each $key grouped by OR clause
*
* @see QueryBuilder::wildcard() for more information
*
* @param string[] $keys the key(s) to wildcard search in.
* @param string|string[] $value the value(s) to wildcard search against.
* @param string $bool severity of the query/filter. [must]|must_not|should
* @param bool $filter if true, DSL query filter section will be used after
* enclosing in a `query` filter.
* @param array $params extra parameters for the query tool
* @return QueryBuilder|static|self
*/
public function multiWildcard(array $keys, $value, $bool = "must", $filter = false, array $params = []) {
$subQuery = $this->orWhere($bool);
foreach ($keys as $key) {
$subQuery->wildcard($key, $value, $bool, true, $params);
}
$subQuery->endSubQuery();
return $this;
}
/**
* Creates wildcard query for each $key/$value pair grouped by OR clause
*
* @see QueryBuilder::wildcard() for more information
*
* @param string[] $keys the key(s) to wildcard search in.
* @param string[] $values the value(s) to wildcard search against.
* @param string $bool severity of the query/filter. [must]|must_not|should
* @param bool $filter if true, DSL query filter section will be used after
* enclosing in a `query` filter.
* @param array $params extra parameters for the query tool
* @return QueryBuilder|static|self
*/
public function multiWildcards(array $keys, array $values, $bool = "must", $filter = false, array $params = []) {
$subQuery = $this->orWhere($bool);
foreach ($keys as $key) {
$subQuery->wildcards($key, $values, $bool, true, $params);
}
$subQuery->endSubQuery();
return $this;
}
/**
* Creates a regexp query part.
*
* It will automatically detect the $key and $value types to create the
* required number of clauses, as follows:
*
* $key $value result
*
* single single {regexp:{$key:{regexp:$value}}}
* single array or:[regexp($key, $value[1]), ...]
* array single or:[regexp($key[1], $value), ...]
* array array or:[regexp($key[1], $value[1]),
* regexp($key[1], $value[2]), ...
* regexp($key[2], $value[1]), ... ]
*
* If $filter is true, it will enclose the regexp query with `query`
* filter and add it to the DSL query filter section instead the query
* section.
*
* @param string|string[] $key the key(s) to regexp search in.
* @param string|string[] $value the value(s) to regexp search against.
* @param string $bool severity of the query/filter. [must]|must_not|should
* @param bool $filter if true, DSL query filter section will be used after
* enclosing in a `query` filter.
* @param array $params extra parameters for the query tool
* @return QueryBuilder|static|self
*/
public function regexp($key, $value, $bool = "must", $filter = false, array $params = []) {
if (is_array($key)) {
return $this->multiRegexp($key, $value, $bool, $filter, $params);
}
if (is_array($value)) {
return $this->regexps($key, $value, $bool, $filter, $params);
}
$this->addBool($this->makeFilteredQuery(["regexp" => [$key => ["value" => $value]]], $filter), $bool, $filter, $params);
return $this;
}
/**
* Creates regexp query for each $value grouped by OR clause
*
* @see QueryBuilder::regexp() for more information
*
* @param string|string[] $key the key(s) to regexp search in.
* @param string[] $values the value(s) to regexp search against.
* @param string $bool severity of the query/filter. [must]|must_not|should
* @param bool $filter if true, DSL query filter section will be used after
* enclosing in a `query` filter.
* @param array $params extra parameters for the query tool
* @return QueryBuilder|static|self
*/
public function regexps($key, array $values, $bool = "must", $filter = false, array $params = []) {
$subQuery = $this->orWhere($bool);
foreach ($values as $value) {
$subQuery->regexp($key, $value, $bool, true, $params);
}
$subQuery->endSubQuery();
return $this;
}
/**
* Creates regexp query for each $key grouped by OR clause
*
* @see QueryBuilder::regexp() for more information
*
* @param string[] $keys the key(s) to regexp search in.
* @param string|string[] $value the value(s) to regexp search against.
* @param string $bool severity of the query/filter. [must]|must_not|should
* @param bool $filter if true, DSL query filter section will be used after
* enclosing in a `query` filter.
* @param array $params extra parameters for the query tool
* @return QueryBuilder|static|self
*/
public function multiRegexp(array $keys, $value, $bool = "must", $filter = false, array $params = []) {
$subQuery = $this->orWhere($bool);
foreach ($keys as $key) {
$subQuery->regexp($key, $value, $bool, true, $params);
}
$subQuery->endSubQuery();
return $this;
}
/**
* Creates regexp query for each $key/$value pair grouped by OR clause
*
* @see QueryBuilder::regexp() for more information
*
* @param string[] $keys the key(s) to regexp search in.
* @param string[] $values the value(s) to regexp search against.
* @param string $bool severity of the query/filter. [must]|must_not|should
* @param bool $filter if true, DSL query filter section will be used after
* enclosing in a `query` filter.
* @param array $params extra parameters for the query tool
* @return QueryBuilder|static|self
*/
public function multiRegexps(array $keys, array $values, $bool = "must", $filter = false, array $params = []) {
$subQuery = $this->orWhere($bool);
foreach ($keys as $key) {
$subQuery->regexps($key, $values, $bool, true, $params);
}
$subQuery->endSubQuery();
return $this;
}
/**
* Creates a prefix query part.
*
* It will automatically detect the $key and $value types to create the
* required number of clauses, as follows:
*
* $key $value result
*
* single single {prefix:{$key:{prefix:$value}}}
* single array or:[prefix($key, $value[1]), ...]
* array single or:[prefix($key[1], $value), ...]
* array array or:[prefix($key[1], $value[1]),
* prefix($key[1], $value[2]), ...
* prefix($key[2], $value[1]), ... ]
*
* If $filter is true, it will enclose the prefix query with `query`
* filter and add it to the DSL query filter section instead the query
* section.
*
* @param string|string[] $key the key(s) to prefix search in.
* @param string|string[] $value the value(s) to prefix search against.
* @param string $bool severity of the query/filter. [must]|must_not|should
* @param bool $filter if true, DSL query filter section will be used after
* enclosing in a `query` filter.
* @param array $params extra parameters for the query tool
* @return QueryBuilder|static|self
*/
public function prefix($key, $value, $bool = "must", $filter = false, array $params = []) {
if (is_array($key)) {
return $this->multiPrefix($key, $value, $bool, $filter, $params);
}
if (is_array($value)) {
return $this->prefixs($key, $value, $bool, $filter, $params);
}
$this->addBool($this->makeFilteredQuery(["prefix" => [$key => $value]], $filter), $bool, $filter, $params);
return $this;
}
/**
* Creates prefix query for each $value grouped by OR clause
*
* @see QueryBuilder::prefix() for more information
*
* @param string|string[] $key the key(s) to prefix search in.
* @param string[] $values the value(s) to prefix search against.
* @param string $bool severity of the query/filter. [must]|must_not|should
* @param bool $filter if true, DSL query filter section will be used after
* enclosing in a `query` filter.
* @param array $params extra parameters for the query tool
* @return QueryBuilder|static|self
*/
public function prefixs($key, array $values, $bool = "must", $filter = false, array $params = []) {
$subQuery = $this->orWhere($bool);
foreach ($values as $value) {
$subQuery->prefix($key, $value, $bool, true, $params);
}
$subQuery->endSubQuery();
return $this;
}
/**
* Creates OR-joined multiple prefix queries for the list of keys
*
* Creates a prefix query for the $value for each key in the $keys
*
* @param array $keys
* @param type $value
* @param type $bool
* @param array $params
* @return QueryBuilder|static|self
*/
public function multiPrefix(array $keys, $value, $bool = "must", $filter = false, array $params = []) {
$subQuery = $this->orWhere($bool);
foreach ($keys as $key) {
$subQuery->prefix($key, $value, $bool, true, $params);
}
$subQuery->endSubQuery();
return $this;
}
/**
* Creates multiple prefixes queries for the list of keys
*
* Two keys and two values will results in 4 prefix queries
*
* @param array $keys
* @param array $values
* @param type $bool
* @param array $params
* @return QueryBuilder|static|self
*/
public function multiPrefixs(array $keys, array $values, $bool = "must", $filter = false, array $params = []) {
$subQuery = $this->orWhere($bool);
foreach ($keys as $key) {
$subQuery->prefixs($key, $values, $bool, true, $params);
}
$subQuery->endSubQuery();
return $this;
}
/**
* A normal term/terms query
*
* It will automatically use term or terms query depending on the type of
* $value parameter whether it is an array or not.
*
* @param type $key
* @param type $value
* @param type $bool
* @param type $filter
* @param array $params
* @return QueryBuilder|static|self
*/
public function term($key, $value, $bool = "must", $filter = false, array $params = []) {
if (is_array($key)) {
return $this->multiTerm($key, $value, $bool, $filter, $params);
}
$tool = "term" . (is_array($value) ? "s" : "");
$this->addBool([$tool => [$key => (is_array($value) ? $value : ["value" => $value])]], $bool, $filter, $params);
return $this;
}
/**
* Multiple OR joined term queries
*
* @param array $keys
* @param type $value
* @param type $bool
* @param type $filter
* @param array $params
* @return QueryBuilder|static|self
*/
public function multiTerm(array $keys, $value, $bool = "must", $filter = false, array $params = []) {
$subQuery = $this->orWhere($bool);
foreach ($keys as $key) {
$subQuery->term($key, $value, $bool, true, $params);
}
$subQuery->endSubQuery();
return $this;
}
/**
* A match query
*
* @param type $key
* @param type $value
* @param type $bool
* @param boolean $filter
* @param array $params
* @return QueryBuilder|static|self
*/
public function match($key, $value, $bool, $filter = false, array $params = []) {
if (is_array($key)) {
return $this->multiMatch($key, $value, $bool, $filter, $params);
}
if (is_array($value)) {
return $this->matches($key, $value, $bool, $filter, $params);
}
$this->addBool($this->makeFilteredQuery(["match" => [$key => ["query" => $value] + $params]], $filter), $bool, $filter);
return $this;
}
/**
* Creates multiple match queries for each value in the array
*
* Queries will be joined by an OR filter
*
* @param string $key the key to create the matches for
* @param array $values a list of values to create a match query for each
* @param type $bool
* @param array $params
* @return QueryBuilder|static|self
*/
public function matches($key, array $values, $bool, $filter = false, array $params = []) {
$subQuery = $this->orWhere($bool);
foreach ($values as $value) {
$subQuery->match($key, $value, $bool, true, $params);
}
$subQuery->endSubQuery();
return $this;
}
/**
* Creates a mutli_match query
*
* @param array $keys keys(fields) of the multimatch
* @param type $value
* @param type $bool
* @param array $params
* @return QueryBuilder|static|self
*/
public function multiMatch(array $keys, $value, $bool, $filter = false, array $params = []) {
$this->addBool($this->makeFilteredQuery(["multi_match" => ["query" => $value, "fields" => $keys] + $params], $filter), $bool, $filter);
return $this;
}
/**
* Creates multiple mutli_match queries for each value in the array
*
* Queries will be joined by an OR filter
*
* @param array $keys keys(fields) of the multimatch
* @param scalar[] $values a list of values to create a multimatch query for
* @param type $bool
* @param array $params
* @return QueryBuilder|static|self
*/
public function multiMatches(array $keys, array $values, $bool, $filter = false, array $params = []) {
$subQuery = $this->orWhere($bool);
foreach ($values as $value) {
$subQuery->multiMatch($keys, $value, $bool, true, $params);
}
$subQuery->endSubQuery();
return $this;
}
/**
* Creates a range query
*
* @param string $key The key to create the range query for
* @param string|string[] $operator lt, gt, lte, gte. Can be an array of mixed lt,gt,lte,gte; and if so, it should match the number of elements in the $value array too.
* @param scalar|scalar[] $value the value for the range comparison. Should be an array of same element count if the $operator is also an array.
* @param string $bool must, should, or must_not
* @param boolean $filter to use the filter part instead of the query part
* @param array $params additional query parameters for the range query
* @return QueryBuilder|static|self
* @throws \BadMethodCallException
*/
public function range($key, $operator, $value, $bool, $filter, array $params = []) {
if (is_array($operator) && !is_array($value) || !is_array($operator) && is_array($value) || is_array($operator) && count($operator) !== count($value)) {
throw new \BadMethodCallException("Operator and value parameters should be both a scalar type or both an array with same number of elements");
}
if (is_array($key)) {
return $this->multiRange($key, $operator, $value, $bool, $filter, $params);
}
$query = [];
$operators = (array) $operator;
$values = (array) $value;
foreach ($operators as $index => $operator) {
$query[$operator] = $values[$index];
}
$this->addBool(["range" => [$key => $query]], $bool, $filter, $params);
return $this;
}
/**
* Creates multiple range queries joined by OR
*
* For each key in the keys, a new range query will be created
*
* @param array $keys keys to create a range query for each of them
* @param string|string[] $operator lt, gt, lte, gte. Can be an array of mixed lt,gt,lte,gte; and if so, it should match the number of elements in the $value array too.
* @param scalar|scalar[] $value the value for the range comparison. Should be an array of same element count if the $operator is also an array.
* @param string $bool must, should, or must_not
* @param boolean $filter to use the filter part instead of the query part
* @param array $params additional query parameters for the range query
* @return QueryBuilder|static|self
*/
public function multiRange(array $keys, $operator, $value, $bool, $filter, array $params = []) {
$subQuery = $this->orWhere($bool);
foreach ($keys as $key) {
$subQuery->range($key, $operator, $value, $bool, true, $params);
}
$subQuery->endSubQuery();
return $this;
}
/**
* Creates an AND filter subquery
*
* @param string $bool must, should, or must_not
* @param array $params extra params for and filter
* @return SubQueryBuilder
*/
public function andWhere($bool = "must", array $params = []) {
$callback = function(SubQueryBuilder $subQuery) use ($bool, $params) {
return $this->_endChildSubQuery("and", $subQuery->toArray(), $bool, $params);
};
return SubQueryBuilder::make($callback);
}
/**
* Creates an OR filter subquery
*
* @param string $bool must, should, or must_not
* @param array $params extra params for or filter
* @return SubQueryBuilder
*/
public function orWhere($bool = "must", array $params = []) {
$callback = function(SubQueryBuilder $subQuery) use ($bool, $params) {
return $this->_endChildSubQuery("or", $subQuery->toArray(), $bool, $params);
};
return SubQueryBuilder::make($callback);
}
/**
* Receives the end signal from the sub query object
*
* @param string $tool [and|or]
* @param array $subQuery
* @param type $bool
* @param array $params
* @return QueryBuilder|static|self
*/
protected function _endChildSubQuery($tool, array $subQuery, $bool, array $params = []) {
$this->addBool([$tool => $subQuery], $bool, true, $params);
return $this;
}
/**
* Checks and creates the required structure
*
* @param string $key A dot [.] separated path to be created/checked
* @return void
*/
protected function assertInitiated($key) {
$current = &$this->query;
$keys = explode(".", $key);
foreach ($keys as $element) {
if (!array_key_exists($element, $current)) {
$current[$element] = [];
}
$current = &$current[$element];
}
}
/**
* Adds a raw must filter part
*
* @param array $query
* @return QueryBuilder|static|self
*/
public function rawMustFilter(array $query) {
$this->addBool($query, "must", true);
return $this;
}
/**
* Adds a raw must not filter part
*
* @param array $query
* @return QueryBuilder|static|self
*/
public function rawMustNotFilter(array $query) {
$this->addBool($query, "must_not", true);
return $this;
}
/**
* Adds a raw should filter part
*
* @param array $query
* @return QueryBuilder|static|self
*/
public function rawShouldFilter(array $query) {
$this->addBool($query, "should", true);
return $this;
}
/**
* Adds a raw must query part
* @param array $query
* @return QueryBuilder|static|self
*/
public function rawMustQuery(array $query) {
$this->addBool($query, "must", false);
return $this;
}
/**
* Adds a raw must_not query part
*
* @param array $query
* @return QueryBuilder|static|self
*/
public function rawMustNotQuery(array $query) {
$this->addBool($query, "must_not", false);
return $this;
}
/**
* Adds a raw bool should query part
*
* @param array $query
* @return QueryBuilder|static|self
*/
public function rawShouldQuery(array $query) {
$this->addBool($query, "should", false);
return $this;
}
/**
* Adds a new bool query part to the query array
*
* @param array $query the bool query part to be added
* @param string $bool the bool group (must, should, must_not)
* @param boolean $filter weither to be added to the filter or the query party
* @param array $params extra parameters for the query part (will be merged into the original part)
*
* @return void
*/
protected function addBool(array $query, $bool, $filter = false, array $params = []) {
$filtered = $filter ? "filter" : "query";
$key = "query.filtered.{$filtered}.bool.{$bool}";
if ($filter) {
$this->assertInitiated($key);
}
$this->query["query"]["filtered"][$filtered]["bool"][$bool][] = array_merge_recursive($query, $params);
}
/**
* Set the from and size (paging) of the results
*
* @param integer $size
* @param integer $from
* @return QueryBuilder|static|self
*/
public function page($size, $from = null) {
if ($from) {
$this->from($from);
}
if ($size) {
$this->size($size);
}
return $this;
}
/**
* Sets the size of the results
*
* @param integer $size
* @return QueryBuilder|static|self
*/
public function size($size) {
$this->assertInitiated("size");
$this->query['size'] = $size;
return $this;
}
/**
* Sets the start index of the results
*
* @param integer $from
* @return QueryBuilder|static|self
*/
public function from($from) {
$this->assertInitiated("from");
$this->query['from'] = $from;
return $this;
}
/**
* Adds a row query part[s] to the current query.
*
* Mainly, it is merging recursivly the $query with the current query
*
* @param array $query
* @return QueryBuilder|static|self
*/
public function raw(array $query) {
array_merge_recursive($this->query, $query);
return $this;
}
protected function makeFilteredQuery(array $queryQuery, $filter = false) {
return $filter ? ["query" => $queryQuery] : $queryQuery;
}
protected function shouldBeFilter($explicit, $implicit) {
return $implicit === true ? true : ($explicit === false ? false : ($explicit || $implicit ? true : $explicit));
}
}