1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace olvlvl\ElasticsearchDSL\Query; |
4
|
|
|
|
5
|
|
|
use olvlvl\ElasticsearchDSL\Helpers; |
6
|
|
|
use olvlvl\ElasticsearchDSL\Query\Term\ExistsQuery; |
7
|
|
|
use olvlvl\ElasticsearchDSL\Query\Term\FuzzyQuery; |
8
|
|
|
use olvlvl\ElasticsearchDSL\Query\Term\IdsQuery; |
9
|
|
|
use olvlvl\ElasticsearchDSL\Query\Term\PrefixQuery; |
10
|
|
|
use olvlvl\ElasticsearchDSL\Query\Term\RangeQuery; |
11
|
|
|
use olvlvl\ElasticsearchDSL\Query\Term\RegexpQuery; |
12
|
|
|
use olvlvl\ElasticsearchDSL\Query\Term\TermQuery; |
13
|
|
|
use olvlvl\ElasticsearchDSL\Query\Term\TermsQuery; |
14
|
|
|
use olvlvl\ElasticsearchDSL\Query\Term\TypeQuery; |
15
|
|
|
use olvlvl\ElasticsearchDSL\Query\Term\WildcardQuery; |
16
|
|
|
|
17
|
|
|
trait TermQueries |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var TermQuery[] |
21
|
|
|
*/ |
22
|
|
|
private $term = []; |
23
|
|
|
|
24
|
|
|
public function term(string $field, $value, callable $config = null) |
25
|
|
|
{ |
26
|
|
|
$this->term[] = $q = new TermQuery($field, $value); |
27
|
|
|
|
28
|
|
|
if ($config) { |
29
|
|
|
$config($q); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var TermsQuery[] |
37
|
|
|
*/ |
38
|
|
|
private $terms = []; |
39
|
|
|
|
40
|
|
|
public function terms(string $field, array $values, callable $config = null) |
41
|
|
|
{ |
42
|
|
|
$this->terms[] = $q = new TermsQuery($field, $values); |
43
|
|
|
|
44
|
|
|
if ($config) { |
45
|
|
|
$config($q); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var RangeQuery[] |
53
|
|
|
*/ |
54
|
|
|
private $range = []; |
55
|
|
|
|
56
|
|
|
public function range(string $field, callable $config = null) |
57
|
|
|
{ |
58
|
|
|
$this->range[] = $q = new RangeQuery($field); |
59
|
|
|
|
60
|
|
|
if ($config) { |
61
|
|
|
$config($q); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var ExistsQuery[] |
69
|
|
|
*/ |
70
|
|
|
private $exists = []; |
71
|
|
|
|
72
|
|
|
public function exists(string $field) |
73
|
|
|
{ |
74
|
|
|
$this->exists[] = new ExistsQuery($field); |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var PrefixQuery[] |
81
|
|
|
*/ |
82
|
|
|
private $prefix = []; |
83
|
|
|
|
84
|
|
|
public function prefix(string $field, $value, callable $config = null) |
85
|
|
|
{ |
86
|
|
|
$this->prefix[] = $q = new PrefixQuery($field, $value); |
87
|
|
|
|
88
|
|
|
if ($config) { |
89
|
|
|
$config($q); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @var WildcardQuery[] |
97
|
|
|
*/ |
98
|
|
|
private $wildcard = []; |
99
|
|
|
|
100
|
|
|
public function wildcard(string $field, $value, callable $config = null) |
101
|
|
|
{ |
102
|
|
|
$this->wildcard[] = $q = new WildcardQuery($field, $value); |
103
|
|
|
|
104
|
|
|
if ($config) { |
105
|
|
|
$config($q); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @var RegexpQuery[] |
113
|
|
|
*/ |
114
|
|
|
private $regexp = []; |
115
|
|
|
|
116
|
|
|
public function regexp(string $field, $value, callable $config = null) |
117
|
|
|
{ |
118
|
|
|
$this->regexp[] = $q = new RegexpQuery($field, $value); |
119
|
|
|
|
120
|
|
|
if ($config) { |
121
|
|
|
$config($q); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @var FuzzyQuery[] |
129
|
|
|
*/ |
130
|
|
|
private $fuzzy = []; |
131
|
|
|
|
132
|
|
|
public function fuzzy(string $field, $value, callable $config = null) |
133
|
|
|
{ |
134
|
|
|
$this->fuzzy[] = $q = new FuzzyQuery($field, $value); |
135
|
|
|
|
136
|
|
|
if ($config) { |
137
|
|
|
$config($q); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @var TypeQuery[] |
145
|
|
|
*/ |
146
|
|
|
private $type = []; |
147
|
|
|
|
148
|
|
|
public function type(string $type) |
149
|
|
|
{ |
150
|
|
|
$this->type[] = new TypeQuery($type); |
151
|
|
|
|
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @var IdsQuery[] |
157
|
|
|
*/ |
158
|
|
|
private $ids = []; |
159
|
|
|
|
160
|
|
|
public function ids(array $ids, callable $config = null) |
161
|
|
|
{ |
162
|
|
|
$this->ids[] = $q = new IdsQuery($ids); |
163
|
|
|
|
164
|
|
|
if ($config) { |
165
|
|
|
$config($q); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
protected function jsonSerializeTermQueries(): array |
172
|
|
|
{ |
173
|
|
|
return Helpers::filter_merge( |
174
|
|
|
$this->term, |
175
|
|
|
$this->terms, |
176
|
|
|
$this->range, |
177
|
|
|
$this->exists, |
178
|
|
|
$this->prefix, |
179
|
|
|
$this->wildcard, |
180
|
|
|
$this->regexp, |
181
|
|
|
$this->fuzzy, |
182
|
|
|
$this->type, |
183
|
|
|
$this->ids |
184
|
|
|
); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|