1
|
|
|
<?php namespace Bardex\Elastic; |
2
|
|
|
|
3
|
|
|
class Where |
4
|
|
|
{ |
5
|
|
|
protected $field; |
6
|
|
|
|
7
|
|
|
protected $query; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
public function __construct(SearchQuery $query) |
11
|
|
|
{ |
12
|
|
|
$this->query = $query; |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param mixed $field |
17
|
|
|
*/ |
18
|
|
|
public function setField($field) |
19
|
|
|
{ |
20
|
|
|
$this->field = $field; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param $value |
25
|
|
|
* @return SearchQuery |
26
|
|
|
*/ |
27
|
|
|
public function equal($value) |
28
|
|
|
{ |
29
|
|
|
$this->query->addFilter('term', [$this->field => $value]); |
30
|
|
|
return $this->query; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Добавить фильтр совпадения хотя бы одного значения из набора, этот фильтр не влияет на поле релевантности _score. |
35
|
|
|
* |
36
|
|
|
* @param $values - массив допустимых значений |
37
|
|
|
* @example $query->where('channel')->in([1,2,3])->where('page.categoryId')->in([10,11]); |
38
|
|
|
* @return SearchQuery; |
|
|
|
|
39
|
|
|
*/ |
40
|
|
View Code Duplication |
public function in(array $values) |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
// потому что ES не понимает дырки в ключах |
43
|
|
|
$values = array_values($values); |
44
|
|
|
$this->query->addFilter('terms', [$this->field => $values]); |
45
|
|
|
return $this->query; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Добавить фильтр вхождения значение в диапазон (обе границы включительно). |
50
|
|
|
* Можно искать по диапазону дат. |
51
|
|
|
* Этот фильтр не влияет на поле релевантности _score. |
52
|
|
|
* |
53
|
|
|
* @param $min - нижняя граница диапазона (включительно) |
54
|
|
|
* @param $max - верхняя граница диапазона (включительно) |
55
|
|
|
* @param $dateFormat - необязательное поле описание формата даты |
56
|
|
|
* @link https://www.elastic.co/guide/en/elasticsearch/reference/5.0/query-dsl-range-query.html |
57
|
|
|
* @return SearchQuery; |
|
|
|
|
58
|
|
|
*/ |
59
|
|
|
public function between($min, $max, $dateFormat = null) |
60
|
|
|
{ |
61
|
|
|
$this->range(['gte' => $min, 'lte' => $max], $dateFormat); |
62
|
|
|
return $this->query; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Добавить фильтр "больше или равно" |
67
|
|
|
* @param $value - значение |
68
|
|
|
* @param null $dateFormat - необязательный формат даты |
69
|
|
|
* @return SearchQuery |
70
|
|
|
*/ |
71
|
|
|
public function greaterOrEqual($value, $dateFormat = null) |
72
|
|
|
{ |
73
|
|
|
$this->range(['gte' => $value], $dateFormat); |
74
|
|
|
return $this->query; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Добавить фильтр "больше чем" |
79
|
|
|
* @param $value - значение |
80
|
|
|
* @param null $dateFormat - необязательный формат даты |
81
|
|
|
* @return SearchQuery |
82
|
|
|
*/ |
83
|
|
|
public function greater($value, $dateFormat = null) |
84
|
|
|
{ |
85
|
|
|
$this->range(['gt' => $value], $dateFormat); |
86
|
|
|
return $this->query; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Добавить фильтр "меньше или равно" |
91
|
|
|
* @param $value - значение |
92
|
|
|
* @param null $dateFormat - необязательный формат даты |
93
|
|
|
* @return SearchQuery |
94
|
|
|
*/ |
95
|
|
|
public function lessOrEqual($value, $dateFormat = null) |
96
|
|
|
{ |
97
|
|
|
$this->range(['lte' => $value], $dateFormat); |
98
|
|
|
return $this->query; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Добавить фильтр "меньше чем" |
104
|
|
|
* @param $value - значение |
105
|
|
|
* @param null $dateFormat - - необязательный формат даты |
106
|
|
|
* @return SearchQuery |
107
|
|
|
*/ |
108
|
|
|
public function less($value, $dateFormat = null) |
109
|
|
|
{ |
110
|
|
|
$this->range(['lt' => $value], $dateFormat); |
111
|
|
|
return $this->query; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
protected function range($params, $dateFormat = null) |
116
|
|
|
{ |
117
|
|
|
if ($dateFormat) { |
118
|
|
|
$params['format'] = $dateFormat; |
119
|
|
|
} |
120
|
|
|
$this->query->addFilter('range', [$this->field => $params]); |
121
|
|
|
return $this->query; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Добавить фильтр полнотекстового поиска, этот фильтр влияет на поле релевантности _score. |
127
|
|
|
* |
128
|
|
|
* @param $text - поисковая фраза |
129
|
|
|
* @return SearchQuery; |
|
|
|
|
130
|
|
|
*/ |
131
|
|
View Code Duplication |
public function match($text) |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
if (is_array($this->field)) { |
134
|
|
|
$this->query->addFilter('multi_match', [ |
135
|
|
|
'query' => $text, |
136
|
|
|
'fields' => $this->field |
137
|
|
|
]); |
138
|
|
|
} else { |
139
|
|
|
$this->query->addFilter('match', [$this->field => $text]); |
140
|
|
|
} |
141
|
|
|
return $this->query; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Поле существует и имеет не null значение |
146
|
|
|
* @return SearchQuery |
147
|
|
|
*/ |
148
|
|
|
public function exists() |
149
|
|
|
{ |
150
|
|
|
$this->query->addFilter('exists', ["field" => $this->field]); |
151
|
|
|
return $this->query; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param $value |
156
|
|
|
* @return SearchQuery |
157
|
|
|
*/ |
158
|
|
|
public function not($value) |
159
|
|
|
{ |
160
|
|
|
$this->query->addNotFilter('term', [$this->field => $value]); |
161
|
|
|
return $this->query; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param $values - массив допустимых значений |
167
|
|
|
* @example $query->where('channel')->notIn([1,2,3]); |
168
|
|
|
* @return SearchQuery; |
|
|
|
|
169
|
|
|
*/ |
170
|
|
View Code Duplication |
public function notIn(array $values) |
|
|
|
|
171
|
|
|
{ |
172
|
|
|
// потому что ES не понимает дырки в ключах |
173
|
|
|
$values = array_values($values); |
174
|
|
|
$this->query->addNotFilter('terms', [$this->field => $values]); |
175
|
|
|
return $this->query; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param $min |
181
|
|
|
* @param $max |
182
|
|
|
* @param null $dateFormat |
183
|
|
|
* @return SearchQuery |
184
|
|
|
*/ |
185
|
|
|
public function notBetween($min, $max, $dateFormat = null) |
186
|
|
|
{ |
187
|
|
|
$params = ['gte' => $min, 'lte' => $max]; |
188
|
|
|
if ($dateFormat) { |
189
|
|
|
$params['format'] = $dateFormat; |
190
|
|
|
} |
191
|
|
|
$this->query->addNotFilter('range', [$this->field => $params]); |
192
|
|
|
return $this->query; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param $text |
197
|
|
|
* @return SearchQuery |
198
|
|
|
*/ |
199
|
|
View Code Duplication |
public function notMatch($text) |
|
|
|
|
200
|
|
|
{ |
201
|
|
|
if (is_array($this->field)) { |
202
|
|
|
$this->query->addNotFilter('multi_match', [ |
203
|
|
|
'query' => $text, |
204
|
|
|
'fields' => $this->field |
205
|
|
|
]); |
206
|
|
|
} else { |
207
|
|
|
$this->query->addNotFilter('match', [$this->field => $text]); |
208
|
|
|
} |
209
|
|
|
return $this->query; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @return SearchQuery |
214
|
|
|
*/ |
215
|
|
|
public function notExists() |
216
|
|
|
{ |
217
|
|
|
$this->query->addNotFilter('exists', ["field" => $this->field]); |
218
|
|
|
return $this->query; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* маска должна указываться в нижнем регистре, см. оф.документацию |
223
|
|
|
* Внимание! низкая производительность |
224
|
|
|
* @param $mask |
225
|
|
|
* @return SearchQuery |
226
|
|
|
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html |
227
|
|
|
*/ |
228
|
|
|
public function wildcard($mask) |
229
|
|
|
{ |
230
|
|
|
$this->query->addFilter('wildcard', [$this->field => $mask]); |
231
|
|
|
return $this->query; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Регулярное выражение должно указываться в нижнем регистре, см. оф.документацию |
236
|
|
|
* Внимание! низкая производительность |
237
|
|
|
* @param $regexp |
238
|
|
|
* @return SearchQuery |
239
|
|
|
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html |
240
|
|
|
*/ |
241
|
|
|
public function regexp($regexp, $flags = null, $maxDeterminizedStates = null) |
242
|
|
|
{ |
243
|
|
|
$filter = ['value' => $regexp]; |
244
|
|
|
|
245
|
|
|
if (null !== $flags) { |
246
|
|
|
$filter['flags'] = $flags; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
if (null !== $maxDeterminizedStates) { |
250
|
|
|
$filter['max_determinized_states'] = $maxDeterminizedStates; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
$this->query->addFilter('regexp', [$this->field => $filter]); |
254
|
|
|
return $this->query; |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.