summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/lib/Elastica/Query/Builder.php
blob: 2a5e8baa28ce08641eff1eb04c8d26e0106520c6 (plain)
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
<?php

namespace Elastica\Query;

use Elastica\Exception\InvalidException;
use Elastica\Exception\JSONParseException;
use Elastica\JSON;

/**
 * Query Builder.
 *
 * @category Xodoa
 * @package Elastica
 * @author Chris Gedrim <chris@gedr.im>
 * @link http://www.elasticsearch.org/
 **/
class Builder extends AbstractQuery
{
    /**
     * Query string.
     *
     * @var string
     */
    private $_string = '{';

    /**
     * Factory method.
     *
     * @param string $string JSON encoded string to use as query.
     *
     * @return \Elastica\Query\Builder
     */
    public static function factory($string = null)
    {
        return new Builder($string);
    }

    /**
     * Constructor
     *
     * @param string $string JSON encoded string to use as query.
     */
    public function __construct($string = null)
    {
        if (! $string == null) {
            $this->_string .= substr($string, 1, -1);
        }
    }

    /**
     * Output the query string.
     *
     * @return string
     */
    public function __toString()
    {
        return rtrim($this->_string, ',').'}';
    }

    /**
     * {@inheritdoc}
     */
    public function toArray()
    {
        try {
            return JSON::parse($this->__toString());
        } catch (JSONParseException $e) {
            throw new InvalidException('The query produced is invalid');
        }
    }

    /**
     * Allow wildcards (*, ?) as the first character in a query.
     *
     * @param boolean $bool Defaults to true.
     *
     * @return \Elastica\Query\Builder
     */
    public function allowLeadingWildcard($bool = true)
    {
        return $this->field('allow_leading_wildcard', (bool) $bool);
    }

    /**
     * Enable best effort analysis of wildcard terms.
     *
     * @param boolean $bool Defaults to true.
     *
     * @return \Elastica\Query\Builder
     */
    public function analyzeWildcard($bool = true)
    {
        return $this->field('analyze_wildcard', (bool) $bool);
    }

    /**
     * Set the analyzer name used to analyze the query string.
     *
     * @param string $analyzer Analyzer to use.
     *
     * @return \Elastica\Query\Builder
     */
    public function analyzer($analyzer)
    {
        return $this->field('analyzer', $analyzer);
    }

    /**
     * Autogenerate phrase queries.
     *
     * @param boolean $bool Defaults to true.
     *
     * @return \Elastica\Query\Builder
     */
    public function autoGeneratePhraseQueries($bool = true)
    {
        return $this->field('auto_generate_phrase_queries', (bool) $bool);
    }

    /**
     * Bool Query.
     *
     * A query that matches documents matching boolean combinations of other queries.
     *
     * The bool query maps to Lucene BooleanQuery.
     *
     * It is built using one or more boolean clauses, each clause with a typed
     * occurrence.
     *
     * The occurrence types are: must, should, must_not.
     *
     * @return \Elastica\Query\Builder
     */
    public function bool()
    {
        return $this->fieldOpen('bool');
    }

    /**
     * Close a 'bool' block.
     *
     * Alias of close() for ease of reading in source.
     *
     * @return \Elastica\Query\Builder
     */
    public function boolClose()
    {
        return $this->fieldClose();
    }

    /**
     * Sets the boost value of the query.
     *
     * @param float $boost Defaults to 1.0.
     *
     * @return \Elastica\Query\Builder
     */
    public function boost($boost = 1.0)
    {
        return $this->field('boost', (float) $boost);
    }

    /**
     * Close a previously opened brace.
     *
     * @return \Elastica\Query\Builder
     */
    public function close()
    {
        $this->_string = rtrim($this->_string, ' ,').'},';

        return $this;
    }

    /**
     * Constant Score Query.
     *
     * A query that wraps a filter or another query and simply returns a constant
     * score equal to the query boost for every document in the filter.
     *
     * Maps to Lucene ConstantScoreQuery.
     *
     * @return \Elastica\Query\Builder
     */
    public function constantScore()
    {
        return $this->fieldOpen('constant_score');
    }

    /**
     * Close a 'constant_score' block.
     *
     * Alias of close() for ease of reading in source.
     *
     * @return \Elastica\Query\Builder
     */
    public function constantScoreClose()
    {
        return $this->fieldClose();
    }

    /**
     * The default field for query terms if no prefix field is specified.
     *
     * @param string $field Defaults to _all.
     *
     * @return \Elastica\Query\Builder
     */
    public function defaultField($field = '_all')
    {
        return $this->field('default_field', $field);
    }

    /**
     * The default operator used if no explicit operator is specified.
     *
     * For example, with a default operator of OR, the query "capital of Hungary"
     * is translated to "capital OR of OR Hungary", and with default operator of
     * AND, the same query is translated to "capital AND of AND Hungary".
     *
     * @param string $operator Defaults to OR.
     *
     * @return \Elastica\Query\Builder
     */
    public function defaultOperator($operator = 'OR')
    {
        return $this->field('default_operator', $operator);
    }

    /**
     * Dis Max Query.
     *
     * A query that generates the union of documents produced by its subqueries,
     * and that scores each document with the maximum score for that document as
     * produced by any subquery, plus a tie breaking increment for any additional
     * matching subqueries.
     *
     * @return \Elastica\Query\Builder
     */
    public function disMax()
    {
        return $this->fieldOpen('dis_max');
    }

    /**
     * Close a 'dis_max' block.
     *
     * Alias of close() for ease of reading in source.
     *
     * @return \Elastica\Query\Builder
     */
    public function disMaxClose()
    {
        return $this->fieldClose();
    }

    /**
     * Enable position increments in result queries.
     *
     * @param boolean $bool Defaults to true.
     *
     * @return \Elastica\Query\Builder
     */
    public function enablePositionIncrements($bool = true)
    {
        return $this->field('enable_position_increments', (bool) $bool);
    }

    /**
     * Enables explanation for each hit on how its score was computed.
     *
     * @param boolean $value Turn on / off explain.
     *
     * @return \Elastica\Query\Builder
     */
    public function explain($value = true)
    {
        return $this->field('explain', $value);
    }

    /**
     * Open 'facets' block.
     *
     * Facets provide aggregated data based on a search query.
     *
     * In the simple case, a facet can return facet counts for various facet
     * values for a specific field.
     *
     * Elasticsearch supports more advanced facet implementations, such as
     * statistical or date histogram facets.
     *
     * @return \Elastica\Query\Builder
     */
    public function facets()
    {
        return $this->fieldOpen('facets');
    }

    /**
     * Close a facets block.
     *
     * Alias of close() for ease of reading in source.
     *
     * @return \Elastica\Query\Builder
     */
    public function facetsClose()
    {
        return $this->close();
    }

    /**
     * Add a specific field / value entry.
     *
     * @param string $name  Field to add.
     * @param mixed  $value Value to set.
     *
     * @return \Elastica\Query\Builder
     */
    public function field($name, $value)
    {
        if (is_bool($value)) {
            $value = '"'. var_export($value, true) . '"';
        } elseif (is_array($value)) {
            $value = '["'.implode('","', $value).'"]';
        } else {
            $value = '"'.$value.'"';
        }

        $this->_string .= '"'.$name.'":'.$value.',';

        return $this;
    }

    /**
     * Close a field block.
     *
     * Alias of close() for ease of reading in source.
     * Passed parameters will be ignored, however they can be useful in source for
     * seeing which field is being closed.
     *
     * Builder::factory()
     *     ->query()
     *     ->range()
     *     ->fieldOpen('created')
     *     ->gte('2011-07-18 00:00:00')
     *     ->lt('2011-07-19 00:00:00')
     *     ->fieldClose('created')
     *     ->rangeClose()
     *     ->queryClose();
     *
     * @return \Elastica\Query\Builder
     */
    public function fieldClose()
    {
        return $this->close();
    }

    /**
     * Open a node for the specified name.
     *
     * @param string $name Field name.
     *
     * @return \Elastica\Query\Builder
     */
    public function fieldOpen($name)
    {
        $this->_string .= '"'.$name.'":';
        $this->open();

        return $this;
    }

    /**
     * Explicitly define fields to return.
     *
     * @param array $fields Array of fields to return.
     *
     * @return \Elastica\Query\Builder
     */
    public function fields(array $fields)
    {
        $this->_string .= '"fields":[';

        foreach ($fields as $field) {
            $this->_string .= '"'.$field.'",';
        }

        $this->_string = rtrim($this->_string, ',').'],';

        return $this;
    }

    /**
     * Open a 'filter' block.
     *
     * @return \Elastica\Query\Builder
     */
    public function filter()
    {
        return $this->fieldOpen('filter');
    }

    /**
     * Close a filter block.
     *
     * @return \Elastica\Query\Builder
     */
    public function filterClose()
    {
        return $this->close();
    }

    /**
     *  Query.
     *
     * @return \Elastica\Query\Builder
     */
    public function filteredQuery()
    {
        return $this->fieldOpen('filtered');
    }

    /**
     * Close a 'filtered_query' block.
     *
     * Alias of close() for ease of reading in source.
     *
     * @return \Elastica\Query\Builder
     */
    public function filteredQueryClose()
    {
        return $this->fieldClose();
    }

    /**
     * Set the from parameter (offset).
     *
     * @param integer $value Result number to start from.
     *
     * @return \Elastica\Query\Builder
     */
    public function from($value = 0)
    {
        return $this->field('from', $value);
    }

    /**
     * Set the minimum similarity for fuzzy queries.
     *
     * @param float $value Defaults to 0.5.
     *
     * @return \Elastica\Query\Builder
     */
    public function fuzzyMinSim($value = 0.5)
    {
        return $this->field('fuzzy_min_sim', (float) $value);
    }

    /**
     * Set the prefix length for fuzzy queries.
     *
     * @param integer $value Defaults to 0.
     *
     * @return \Elastica\Query\Builder
     */
    public function fuzzyPrefixLength($value = 0)
    {
        return $this->field('fuzzy_prefix_length', (int) $value);
    }

    /**
     * Add a greater than (gt) clause.
     *
     * Used in range blocks.
     *
     * @param mixed $value Value to be gt.
     *
     * @return \Elastica\Query\Builder
     */
    public function gt($value)
    {
        return $this->field('gt', $value);
    }

    /**
     * Add a greater than or equal to (gte) clause.
     *
     * Used in range blocks.
     *
     * @param mixed $value Value to be gte to.
     *
     * @return \Elastica\Query\Builder
     */
    public function gte($value)
    {
        return $this->field('gte', $value);
    }

    /**
     * Automatically lower-case terms of wildcard, prefix, fuzzy, and range queries.
     *
     * @param boolean $bool Defaults to true.
     *
     * @return \Elastica\Query\Builder
     */
    public function lowercaseExpandedTerms($bool = true)
    {
        return $this->field('lowercase_expanded_terms', (bool) $bool);
    }

    /**
     * Add a less than (lt) clause.
     *
     * Used in range blocks.
     *
     * @param mixed $value Value to be lt.
     *
     * @return \Elastica\Query\Builder
     */
    public function lt($value)
    {
        return $this->field('lt', $value);
    }

    /**
     * Add a less than or equal to (lte) clause.
     *
     * Used in range blocks.
     *
     * @param mixed $value Value to be lte to.
     *
     * @return \Elastica\Query\Builder
     */
    public function lte($value)
    {
        return $this->field('lte', $value);
    }

    /**
     * Match All Query.
     *
     * A query that matches all documents.
     *
     * Maps to Lucene MatchAllDocsQuery.
     *
     * @param float $boost Boost to use.
     *
     * @return \Elastica\Query\Builder
     */
    public function matchAll($boost = null)
    {
        $this->fieldOpen('match_all');

        if ( ! $boost == null && is_numeric($boost)) {
            $this->field('boost', (float) $boost);
        }

        return $this->close();
    }

    /**
     * The minimum number of should clauses to match.
     *
     * @param integer $minimum Minimum number that should match.
     *
     * @return \Elastica\Query\Builder
     */
    public function minimumNumberShouldMatch($minimum)
    {
        return $this->field('minimum_number_should_match', (int) $minimum);
    }

    /**
     * The clause (query) must appear in matching documents.
     *
     * @return \Elastica\Query\Builder
     */
    public function must()
    {
        return $this->fieldOpen('must');
    }

    /**
     * Close a 'must' block.
     *
     * Alias of close() for ease of reading in source.
     *
     * @return \Elastica\Query\Builder
     */
    public function mustClose()
    {
        return $this->fieldClose();
    }

    /**
     * The clause (query) must not appear in the matching documents.
     *
     * Note that it is not possible to search on documents that only consists of
     * a must_not clauses.
     *
     * @return \Elastica\Query\Builder
     */
    public function mustNot()
    {
        return $this->fieldOpen('must_not');
    }

    /**
     * Close a 'must_not' block.
     *
     * Alias of close() for ease of reading in source.
     *
     * @return \Elastica\Query\Builder
     */
    public function mustNotClose()
    {
        return $this->fieldClose();
    }

    /**
     * Add an opening brace.
     *
     * @return \Elastica\Query\Builder
     */
    public function open()
    {
        $this->_string .= '{';

        return $this;
    }

    /**
     * Sets the default slop for phrases.
     *
     * If zero, then exact phrase matches are required.
     *
     * @param integer $value Defaults to 0.
     *
     * @return \Elastica\Query\Builder
     */
    public function phraseSlop($value = 0)
    {
        return $this->field('phrase_slop', (int) $value);
    }

    /**
     *  Query.
     *
     * @return \Elastica\Query\Builder
     */
    public function prefix()
    {
        return $this->fieldOpen('prefix');
    }

    /**
     * Close a 'prefix' block.
     *
     * Alias of close() for ease of reading in source.
     *
     * @return \Elastica\Query\Builder
     */
    public function prefixClose()
    {
        return $this->fieldClose();
    }

    /**
     * Queries to run within a dis_max query.
     *
     * @param array $queries Array of queries.
     *
     * @return \Elastica\Query\Builder
     */
    public function queries(array $queries)
    {
        $this->_string .= '"queries":[';

        foreach ($queries as $query) {
            $this->_string .= $query.',';
        }

        $this->_string = rtrim($this->_string, ' ,').'],';

        return $this;
    }

    /**
     * Open a query block.
     *
     * @return \Elastica\Query\Builder
     */
    public function query()
    {
        return $this->fieldOpen('query');
    }

    /**
     * Close a query block.
     *
     * Alias of close() for ease of reading in source.
     *
     * @return \Elastica\Query\Builder
     */
    public function queryClose()
    {
        return $this->close();
    }

    /**
     * Query String Query.
     *
     * A query that uses a query parser in order to parse its content
     *
     * @return \Elastica\Query\Builder
     */
    public function queryString()
    {
        return $this->fieldOpen('query_string');
    }

    /**
     * Close a 'query_string' block.
     *
     * Alias of close() for ease of reading in source.
     *
     * @return \Elastica\Query\Builder
     */
    public function queryStringClose()
    {
        return $this->fieldClose();
    }

    /**
     * Open a range block.
     *
     * @return \Elastica\Query\Builder
     */
    public function range()
    {
        return $this->fieldOpen('range');
    }

    /**
     * Close a range block.
     *
     * Alias of close() for ease of reading in source.
     *
     * @return \Elastica\Query\Builder
     */
    public function rangeClose()
    {
        return $this->close();
    }

    /**
     * The clause (query) should appear in the matching document.
     *
     * A boolean query with no must clauses, one or more should clauses must
     * match a document.
     *
     * @return \Elastica\Query\Builder
     */
    public function should()
    {
        return $this->fieldOpen('should');
    }

    /**
     * Close a 'should' block.
     *
     * Alias of close() for ease of reading in source.
     *
     * @return \Elastica\Query\Builder
     */
    public function shouldClose()
    {
        return $this->fieldClose();
    }

    /**
     * Set the size parameter (number of records to return).
     *
     * @param integer $value Number of records to return.
     *
     * @return \Elastica\Query\Builder
     */
    public function size($value = 10)
    {
        return $this->field('size', $value);
    }

    /**
     * Allows to add one or more sort on specific fields.
     *
     * @return \Elastica\Query\Builder
     */
    public function sort()
    {
        return $this->fieldOpen('sort');
    }

    /**
     * Close a sort block.
     *
     * Alias of close() for ease of reading in source.
     *
     * @return \Elastica\Query\Builder
     */
    public function sortClose()
    {
        return $this->close();
    }

    /**
     * Add a field to sort on.
     *
     * @param string  $name    Field to sort.
     * @param boolean $reverse Reverse direction.
     *
     * @return \Elastica\Query\Builder
     */
    public function sortField($name, $reverse = false)
    {
        return $this
            ->fieldOpen('sort')
            ->fieldOpen($name)
            ->field('reverse', $reverse)
            ->close()
            ->close();
    }

    /**
     * Sort on multiple fields
     *
     * @param array $fields Associative array where the keys are field names to sort on, and the
     *                      values are the sort order: "asc" or "desc"
     *
     * @return \Elastica\Query\Builder
     */
    public function sortFields(array $fields)
    {
        $this->_string .= '"sort":[';

        foreach ($fields as $fieldName => $order) {
            $this->_string .= '{"'.$fieldName.'":"'.$order.'"},';
        }

        $this->_string = rtrim($this->_string, ',') . '],';

        return $this;
    }

    /**
     * Term Query.
     *
     * Matches documents that have fields that contain a term (not analyzed).
     *
     * The term query maps to Lucene TermQuery.
     *
     * @return \Elastica\Query\Builder
     */
    public function term()
    {
        return $this->fieldOpen('term');
    }

    /**
     * Close a 'term' block.
     *
     * Alias of close() for ease of reading in source.
     *
     * @return \Elastica\Query\Builder
     */
    public function termClose()
    {
        return $this->fieldClose();
    }

    /**
     * Open a 'text_phrase' block.
     *
     * @return \Elastica\Query\Builder
     */
    public function textPhrase()
    {
        return $this->fieldOpen('text_phrase');
    }

    /**
     * Close a 'text_phrase' block.
     *
     * @return \Elastica\Query\Builder
     */
    public function textPhraseClose()
    {
        return $this->close();
    }

    /**
     * When using dis_max, the disjunction max tie breaker.
     *
     * @param float $multiplier Multiplier to use.
     *
     * @return \Elastica\Query\Builder
     */
    public function tieBreakerMultiplier($multiplier)
    {
        return $this->field('tie_breaker_multiplier', (float) $multiplier);
    }

    /**
     *  Query.
     *
     * @return \Elastica\Query\Builder
     */
    public function wildcard()
    {
        return $this->fieldOpen('wildcard');
    }

    /**
     * Close a 'wildcard' block.
     *
     * Alias of close() for ease of reading in source.
     *
     * @return \Elastica\Query\Builder
     */
    public function wildcardClose()
    {
        return $this->fieldClose();
    }
}