1<?php
2
3namespace Elastica\QueryBuilder\DSL;
4
5use Elastica\Query\AbstractQuery;
6use Elastica\Query\AbstractSpanQuery;
7use Elastica\Query as BaseQuery;
8use Elastica\Query\BoolQuery;
9use Elastica\Query\Boosting;
10use Elastica\Query\Common;
11use Elastica\Query\ConstantScore;
12use Elastica\Query\DisMax;
13use Elastica\Query\DistanceFeature;
14use Elastica\Query\Exists;
15use Elastica\Query\FunctionScore;
16use Elastica\Query\Fuzzy;
17use Elastica\Query\GeoBoundingBox;
18use Elastica\Query\GeoDistance;
19use Elastica\Query\GeoPolygon;
20use Elastica\Query\HasChild;
21use Elastica\Query\HasParent;
22use Elastica\Query\Ids;
23use Elastica\Query\MatchAll;
24use Elastica\Query\MatchNone;
25use Elastica\Query\MatchPhrase;
26use Elastica\Query\MatchPhrasePrefix;
27use Elastica\Query\MatchQuery;
28use Elastica\Query\MoreLikeThis;
29use Elastica\Query\MultiMatch;
30use Elastica\Query\Nested;
31use Elastica\Query\ParentId;
32use Elastica\Query\Percolate;
33use Elastica\Query\Prefix;
34use Elastica\Query\QueryString;
35use Elastica\Query\Range;
36use Elastica\Query\Regexp;
37use Elastica\Query\SimpleQueryString;
38use Elastica\Query\SpanContaining;
39use Elastica\Query\SpanFirst;
40use Elastica\Query\SpanMulti;
41use Elastica\Query\SpanNear;
42use Elastica\Query\SpanNot;
43use Elastica\Query\SpanOr;
44use Elastica\Query\SpanTerm;
45use Elastica\Query\SpanWithin;
46use Elastica\Query\Term;
47use Elastica\Query\Terms;
48use Elastica\Query\TermsSet;
49use Elastica\Query\Wildcard;
50use Elastica\QueryBuilder\DSL;
51use Elastica\Script\AbstractScript;
52
53/**
54 * elasticsearch query DSL.
55 *
56 * @author Manuel Andreo Garcia <andreo.garcia@googlemail.com>
57 *
58 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-queries.html
59 */
60class Query implements DSL
61{
62    /**
63     * must return type for QueryBuilder usage.
64     */
65    public function getType(): string
66    {
67        return self::TYPE_QUERY;
68    }
69
70    /**
71     * match query.
72     *
73     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html
74     *
75     * @param mixed $values
76     */
77    public function match(?string $field = null, $values = null): MatchQuery
78    {
79        return new MatchQuery($field, $values);
80    }
81
82    /**
83     * multi match query.
84     *
85     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html
86     */
87    public function multi_match(): MultiMatch
88    {
89        return new MultiMatch();
90    }
91
92    /**
93     * bool query.
94     *
95     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html
96     */
97    public function bool(): BoolQuery
98    {
99        return new BoolQuery();
100    }
101
102    /**
103     * boosting query.
104     *
105     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-boosting-query.html
106     */
107    public function boosting(): Boosting
108    {
109        return new Boosting();
110    }
111
112    /**
113     * common terms query.
114     *
115     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-common-terms-query.html
116     * @deprecated since version 7.1.3, use the "match()" method instead.
117     *
118     * @param float $cutoffFrequency percentage in decimal form (.001 == 0.1%)
119     */
120    public function common_terms(string $field, string $query, float $cutoffFrequency): Common
121    {
122        \trigger_deprecation('ruflin/elastica', '7.1.3', 'The "%s()" method is deprecated, use "match()" instead. It will be removed in 8.0.', __METHOD__);
123
124        return new Common($field, $query, $cutoffFrequency);
125    }
126
127    /**
128     * constant score query.
129     *
130     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-constant-score-query.html
131     */
132    public function constant_score(?AbstractQuery $filter = null): ConstantScore
133    {
134        return new ConstantScore($filter);
135    }
136
137    /**
138     * dis max query.
139     *
140     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-dis-max-query.html
141     */
142    public function dis_max(): DisMax
143    {
144        return new DisMax();
145    }
146
147    /**
148     * distance feature query.
149     *
150     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-distance-feature-query.html
151     *
152     * @param array|string $origin
153     */
154    public function distance_feature(string $field, $origin, string $pivot): DistanceFeature
155    {
156        return new DistanceFeature($field, $origin, $pivot);
157    }
158
159    /**
160     * function score query.
161     *
162     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html
163     */
164    public function function_score(): FunctionScore
165    {
166        return new FunctionScore();
167    }
168
169    /**
170     * fuzzy query.
171     *
172     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html
173     *
174     * @param string|null $value String to search for
175     */
176    public function fuzzy(?string $fieldName = null, ?string $value = null): Fuzzy
177    {
178        return new Fuzzy($fieldName, $value);
179    }
180
181    /**
182     * geo bounding box query.
183     *
184     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-bounding-box-query.html
185     */
186    public function geo_bounding_box(string $key, array $coordinates): GeoBoundingBox
187    {
188        return new GeoBoundingBox($key, $coordinates);
189    }
190
191    /**
192     * geo distance query.
193     *
194     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-query.html
195     *
196     * @param array|string $location
197     */
198    public function geo_distance(string $key, $location, string $distance): GeoDistance
199    {
200        return new GeoDistance($key, $location, $distance);
201    }
202
203    /**
204     * geo polygon query.
205     *
206     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-polygon-query.html
207     */
208    public function geo_polygon(string $key, array $points): GeoPolygon
209    {
210        return new GeoPolygon($key, $points);
211    }
212
213    /**
214     * has child query.
215     *
216     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-child-query.html
217     *
218     * @param AbstractQuery|BaseQuery|string $query
219     * @param string|null                    $type  Parent document type
220     */
221    public function has_child($query, ?string $type = null): HasChild
222    {
223        return new HasChild($query, $type);
224    }
225
226    /**
227     * has parent query.
228     *
229     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-parent-query.html
230     *
231     * @param AbstractQuery|BaseQuery|string $query
232     * @param string                         $type  Parent document type
233     */
234    public function has_parent($query, string $type): HasParent
235    {
236        return new HasParent($query, $type);
237    }
238
239    /**
240     * ids query.
241     *
242     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-ids-query.html
243     */
244    public function ids(array $ids = []): Ids
245    {
246        return new Ids($ids);
247    }
248
249    /**
250     * match all query.
251     *
252     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html
253     */
254    public function match_all(): MatchAll
255    {
256        return new MatchAll();
257    }
258
259    /**
260     * match none query.
261     *
262     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html#query-dsl-match-none-query
263     */
264    public function match_none(): MatchNone
265    {
266        return new MatchNone();
267    }
268
269    /**
270     * match phrase query.
271     *
272     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase.html
273     *
274     * @param mixed|null $values
275     */
276    public function match_phrase(?string $field = null, $values = null): MatchPhrase
277    {
278        return new MatchPhrase($field, $values);
279    }
280
281    /**
282     * match phrase prefix query.
283     *
284     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase-prefix.html
285     *
286     * @param mixed|null $values
287     */
288    public function match_phrase_prefix(?string $field = null, $values = null): MatchPhrasePrefix
289    {
290        return new MatchPhrasePrefix($field, $values);
291    }
292
293    /**
294     * more like this query.
295     *
296     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-mlt-query.html
297     */
298    public function more_like_this(): MoreLikeThis
299    {
300        return new MoreLikeThis();
301    }
302
303    /**
304     * nested query.
305     *
306     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html
307     */
308    public function nested(): Nested
309    {
310        return new Nested();
311    }
312
313    /**
314     * @param int|string $id
315     */
316    public function parent_id(string $type, $id, bool $ignoreUnmapped = false): ParentId
317    {
318        return new ParentId($type, $id, $ignoreUnmapped);
319    }
320
321    /**
322     * prefix query.
323     *
324     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-prefix-query.html
325     *
326     * @param array $prefix Prefix array
327     */
328    public function prefix(array $prefix = []): Prefix
329    {
330        return new Prefix($prefix);
331    }
332
333    /**
334     * query string query.
335     *
336     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html
337     *
338     * @param string $queryString OPTIONAL Query string for object
339     */
340    public function query_string(string $queryString = ''): QueryString
341    {
342        return new QueryString($queryString);
343    }
344
345    /**
346     * simple_query_string query.
347     *
348     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html
349     */
350    public function simple_query_string(string $query, array $fields = []): SimpleQueryString
351    {
352        return new SimpleQueryString($query, $fields);
353    }
354
355    /**
356     * range query.
357     *
358     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html
359     */
360    public function range(?string $fieldName = null, array $args = []): Range
361    {
362        return new Range($fieldName, $args);
363    }
364
365    /**
366     * regexp query.
367     *
368     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html
369     */
370    public function regexp(string $key = '', ?string $value = null, float $boost = 1.0): Regexp
371    {
372        return new Regexp($key, $value, $boost);
373    }
374
375    /**
376     * span first query.
377     *
378     * @param AbstractQuery|array $match
379     *
380     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-first-query.html
381     */
382    public function span_first($match = null, ?int $end = null): SpanFirst
383    {
384        return new SpanFirst($match, $end);
385    }
386
387    /**
388     * span multi term query.
389     *
390     * @param AbstractQuery|array $match
391     *
392     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-multi-term-query.html
393     */
394    public function span_multi_term($match = null): SpanMulti
395    {
396        return new SpanMulti($match);
397    }
398
399    /**
400     * span near query.
401     *
402     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-near-query.html
403     */
404    public function span_near(array $clauses = [], int $slop = 1, bool $inOrder = false): SpanNear
405    {
406        return new SpanNear($clauses, $slop, $inOrder);
407    }
408
409    /**
410     * span not query.
411     *
412     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-not-query.html
413     */
414    public function span_not(?AbstractSpanQuery $include = null, ?AbstractSpanQuery $exclude = null): SpanNot
415    {
416        return new SpanNot($include, $exclude);
417    }
418
419    /**
420     * span_or query.
421     *
422     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-or-query.html
423     */
424    public function span_or(array $clauses = []): SpanOr
425    {
426        return new SpanOr($clauses);
427    }
428
429    /**
430     * span_term query.
431     *
432     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-term-query.html
433     */
434    public function span_term(array $term = []): SpanTerm
435    {
436        return new SpanTerm($term);
437    }
438
439    /**
440     * span_containing query.
441     *
442     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-containing-query.html
443     */
444    public function span_containing(?AbstractSpanQuery $little = null, ?AbstractSpanQuery $big = null): SpanContaining
445    {
446        return new SpanContaining($little, $big);
447    }
448
449    /**
450     * span_within query.
451     *
452     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-within-query.html
453     */
454    public function span_within(?AbstractSpanQuery $little = null, ?AbstractSpanQuery $big = null): SpanWithin
455    {
456        return new SpanWithin($little, $big);
457    }
458
459    /**
460     * term query.
461     *
462     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html
463     */
464    public function term(array $term = []): Term
465    {
466        return new Term($term);
467    }
468
469    /**
470     * terms query.
471     *
472     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html
473     */
474    public function terms(string $field, array $terms = []): Terms
475    {
476        return new Terms($field, $terms);
477    }
478
479    /**
480     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-set-query.html
481     *
482     * @param array<bool|float|int|string> $terms
483     * @param AbstractScript|string        $minimumShouldMatch
484     */
485    public function terms_set(string $field, array $terms, $minimumShouldMatch): TermsSet
486    {
487        return new TermsSet($field, $terms, $minimumShouldMatch);
488    }
489
490    /**
491     * wildcard query.
492     *
493     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html
494     */
495    public function wildcard(string $field, string $value, float $boost = 1.0): Wildcard
496    {
497        return new Wildcard($field, $value, $boost);
498    }
499
500    /**
501     * exists query.
502     *
503     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html
504     */
505    public function exists(string $field): Exists
506    {
507        return new Exists($field);
508    }
509
510    /**
511     * type query.
512     *
513     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-percolate-query.html
514     */
515    public function percolate(): Percolate
516    {
517        return new Percolate();
518    }
519}
520