1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Soupmix; |
4
|
|
|
/* |
5
|
|
|
Elasticsearch Adapter |
6
|
|
|
*/ |
7
|
|
|
Use Elasticsearch\Client; |
8
|
|
|
|
9
|
|
|
final class ElasticSearch implements Base |
10
|
|
|
{ |
11
|
|
|
private $conn = null; |
12
|
|
|
private $index = null; |
13
|
|
|
private $esMajorVersion = 2; |
14
|
|
|
private static $filteredOrBool = [2 => 'filtered', 5 => 'bool', 6 => 'bool']; |
15
|
|
|
|
16
|
|
|
private static $operators = [ |
17
|
|
|
'range' => ['gt', 'gte', 'lt', 'lte'], |
18
|
|
|
'standart' => ['prefix', 'regexp', 'wildcard'], |
19
|
|
|
'BooleanQueryLevel' => ['not'], |
20
|
|
|
'special' => ['in'] |
21
|
|
|
]; |
22
|
3 |
|
|
23
|
|
|
public function __construct($config, Client $client) |
24
|
3 |
|
{ |
25
|
3 |
|
$this->index = $config['db_name']; |
26
|
3 |
|
$this->conn = $client; |
27
|
|
|
$this->esMajorVersion = substr($client->info()['version']['number'],0,1); |
|
|
|
|
28
|
3 |
|
} |
29
|
|
|
|
30
|
3 |
|
|
31
|
3 |
|
public function getConnection() |
32
|
|
|
{ |
33
|
|
|
return $this->conn; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function create(string $collection, array $fields) |
37
|
3 |
|
{ |
38
|
|
|
$params = ['index' => $this->index]; |
39
|
3 |
|
try { |
40
|
|
|
if (!$this->conn->indices()->exists($params)) { |
41
|
3 |
|
$this->conn->indices()->create($params); |
42
|
3 |
|
} |
43
|
|
|
$params['type'] = $collection; |
44
|
1 |
|
$params['body'] = [$collection => ['properties' => $fields]]; |
45
|
|
|
$this->conn->indices()->putMapping($params); |
46
|
2 |
|
} catch (\Exception $e) { |
47
|
|
|
// This ignore the error |
48
|
|
|
return false; |
49
|
|
|
} |
50
|
|
|
return true; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function drop(string $collection) |
54
|
|
|
{ |
55
|
|
|
$params = ['index' => $this->index, 'type' => $collection]; |
|
|
|
|
56
|
|
|
try { |
|
|
|
|
57
|
2 |
|
//$this->truncate($collection); |
|
|
|
|
58
|
|
|
//$mappingData = $this->conn->indices()->getMapping($params); |
|
|
|
|
59
|
2 |
|
//$mapping = $mappingData[$this->index]['mappings'][$collection]; |
|
|
|
|
60
|
2 |
|
//$ma = $this->conn->indices()->deleteMapping($params); |
|
|
|
|
61
|
2 |
|
|
62
|
2 |
|
} catch (\Exception $e) { |
|
|
|
|
63
|
|
|
// This ignore the error |
64
|
2 |
|
return false; |
65
|
2 |
|
} |
66
|
2 |
|
return true; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function truncate(string $collection) |
70
|
|
|
{ |
71
|
|
|
$params = ['index' => $this->index, 'type' => $collection, 'body' => ['query' => ['bool' =>['match_all' => []]]]]; |
72
|
|
|
$this->conn->deleteByQuery($params); |
73
|
|
|
} |
74
|
1 |
|
|
75
|
|
|
public function createIndexes(string $collection, array $indexes) |
76
|
1 |
|
{} |
77
|
1 |
|
|
78
|
1 |
|
public function insert(string $collection, array $values) |
79
|
|
|
{ |
80
|
|
|
$params = []; |
81
|
1 |
|
$params['body'] = $values; |
82
|
|
|
$params['index'] = $this->index; |
83
|
|
|
$params['type'] = $collection; |
84
|
|
|
try { |
85
|
|
|
$result = $this->conn->index($params); |
86
|
|
|
} catch (\Exception $e) { |
87
|
|
|
return; |
88
|
|
|
} |
89
|
|
|
if ( |
90
|
|
|
array_key_exists('created', $result) |
91
|
|
|
|| (array_key_exists('result', $result) && $result['result'] === 'created') |
92
|
|
|
) { |
93
|
|
|
return $result['_id']; |
94
|
|
|
} |
95
|
|
|
return null; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function get(string $collection, $docId) |
99
|
|
|
{ |
100
|
|
|
$params = []; |
101
|
1 |
|
$params['index'] = $this->index; |
102
|
1 |
|
$params['type'] = $collection; |
103
|
|
|
|
104
|
1 |
|
try { |
105
|
1 |
|
if (is_array($docId)) { |
106
|
1 |
|
$params['body'] = [ |
107
|
|
|
'query' => [ |
108
|
|
|
self::$filteredOrBool[$this->esMajorVersion] => [ |
109
|
|
|
'filter' => [ |
110
|
|
|
'ids' => ['values' => $docId] |
111
|
|
|
], |
112
|
|
|
], |
113
|
|
|
], |
114
|
|
|
]; |
115
|
|
|
$results = $this->conn->search($params); |
116
|
|
|
if ($results['hits']['total'] === 0) { |
117
|
|
|
return; |
118
|
|
|
} |
119
|
|
|
$result = []; |
120
|
|
|
foreach ($results['hits']['hits'] as $item){ |
121
|
|
|
$result[$item['_id']]=$item['_source']; |
122
|
|
|
} |
123
|
|
|
return $result; |
124
|
|
|
} else { |
125
|
|
|
$params['id'] = $docId; |
126
|
|
|
$result = $this->conn->get($params); |
127
|
|
|
} |
128
|
|
|
if ($result['found']) { |
129
|
|
|
$result['_source']['_id'] = $result['_id']; |
130
|
|
|
return $result['_source']; |
131
|
|
|
} else { |
132
|
|
|
return; |
133
|
|
|
} |
134
|
|
|
} catch (\Exception $e) { |
135
|
|
|
return; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function update(string $collection, array $filter, array $values) |
140
|
|
|
{ |
141
|
2 |
|
$docs = $this->find($collection, $filter, ['_id']); |
142
|
|
|
if ($docs['total'] ===0) { |
143
|
2 |
|
return 0; |
144
|
2 |
|
} |
145
|
2 |
|
$params = []; |
146
|
2 |
|
$params['index'] = $this->index; |
147
|
2 |
|
$params['type'] = $collection; |
148
|
|
|
$modified_count = 0; |
149
|
2 |
|
foreach ($docs['data'] as $doc) { |
150
|
2 |
|
$params['id'] = $doc['_id']; |
151
|
|
|
$params['body']['doc'] = $values; |
152
|
|
|
try { |
153
|
2 |
|
$this->conn->update($params); |
154
|
2 |
|
++$modified_count; |
155
|
|
|
} catch (\Exception $e) { |
156
|
|
|
// throw new \Exception($e->getMessage()); |
|
|
|
|
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $modified_count; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function delete(string $collection, array $filter) |
164
|
|
|
{ |
165
|
|
|
if (isset($filter['_id'])) { |
166
|
|
|
$params = []; |
167
|
|
|
$params['index'] = $this->index; |
168
|
|
|
$params['type'] = $collection; |
169
|
|
|
$params['id'] = $filter['_id']; |
170
|
|
|
try { |
171
|
|
|
$result = $this->conn->delete($params); |
172
|
|
|
} catch (\Exception $e) { |
173
|
|
|
return 0; |
174
|
|
|
} |
175
|
|
|
if ( |
176
|
|
|
array_key_exists('found', $result) |
177
|
|
|
|| (array_key_exists('result', $result) && $result['result'] === 'deleted') |
178
|
|
|
) { |
179
|
|
|
return 1; |
180
|
|
|
} |
181
|
1 |
|
return 0; |
182
|
|
|
} |
183
|
1 |
|
$params = []; |
184
|
1 |
|
$params['index'] = $this->index; |
185
|
1 |
|
$params['type'] = $collection; |
186
|
1 |
|
$params['fields'] = '_id'; |
187
|
1 |
|
$results = $this->find($collection, $filter, ['_id'], null, 0, 10000); |
188
|
1 |
|
$deleteCount = 0; |
189
|
1 |
|
if ($results['total']>0) { |
190
|
|
|
$dParams = []; |
191
|
|
|
$dParams['index'] = $this->index; |
192
|
|
|
$dParams['type'] = $collection; |
193
|
1 |
|
foreach ($results['data'] as $result) { |
194
|
1 |
|
$dParams['id'] = $result['_id']; |
195
|
1 |
|
try { |
196
|
1 |
|
$result = $this->conn->delete($dParams); |
197
|
|
|
} catch (\Exception $e) { |
198
|
1 |
|
return 0; |
199
|
1 |
|
} |
200
|
1 |
|
if ( |
201
|
|
|
array_key_exists('found', $result) |
202
|
|
|
|| (array_key_exists('result', $result) && $result['result'] === 'deleted') |
203
|
|
|
) { |
204
|
1 |
|
$deleteCount++; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
return $deleteCount; |
208
|
|
|
} |
209
|
|
|
return 0; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function find( |
213
|
1 |
|
string $collection, |
214
|
|
|
?array $filters, |
215
|
|
|
?array $fields = null, |
216
|
|
|
?array $sort = null, |
217
|
1 |
|
int $start = 0, |
218
|
1 |
|
int$limit = 25 |
219
|
1 |
|
) : array |
220
|
|
|
{ |
221
|
|
|
$return_type = '_source'; |
222
|
1 |
|
$params = []; |
223
|
1 |
|
$params['index'] = $this->index; |
224
|
|
|
$params['type'] = $collection; |
225
|
|
|
$params['body'] = []; |
226
|
1 |
|
if ($filters!==null) { |
227
|
|
|
$filters = self::buildFilter($filters); |
228
|
|
|
$params['body'] = [ |
229
|
|
|
'query' => [ |
230
|
1 |
|
self::$filteredOrBool[$this->esMajorVersion] => [ |
231
|
1 |
|
'filter' => [ |
232
|
1 |
|
'bool' => $filters, |
233
|
|
|
], |
234
|
|
|
], |
235
|
|
|
], |
236
|
|
|
]; |
237
|
1 |
|
} |
238
|
1 |
|
$count = $this->conn->count($params); |
239
|
1 |
|
if ($sort !== null) { |
240
|
1 |
|
$params['body']['sort'] = []; |
241
|
|
|
foreach ($sort as $sort_key => $sort_dir) { |
242
|
|
|
$params['body']['sort'][] = [$sort_key => $sort_dir]; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
if ($fields !== null) { |
246
|
|
|
$params['_source'] = $fields; |
247
|
|
|
} |
248
|
1 |
|
$params['from'] = (int) $start; |
249
|
|
|
$params['size'] = (int) $limit; |
250
|
1 |
|
|
251
|
1 |
|
$return = $this->conn->search($params); |
252
|
1 |
|
|
253
|
1 |
|
if ($return['hits']['total'] === 0) { |
254
|
1 |
|
return ['total' => 0, 'data' => null]; |
255
|
1 |
|
} |
256
|
1 |
|
elseif ($limit === 1) { |
257
|
1 |
|
$return['hits']['hits'][0][$return_type]['_id'] = $return['hits']['hits'][0]['_id']; |
258
|
1 |
|
return ['total' => 1, 'data' => $return['hits']['hits'][0][$return_type]]; |
259
|
1 |
|
} |
260
|
|
|
$result = []; |
261
|
1 |
|
foreach ($return['hits']['hits'] as $item) { |
262
|
1 |
|
|
263
|
|
|
if (($return_type === 'fields') && ($fields !== ['_id'])) { |
264
|
|
|
foreach ($item[$return_type]as $k => $v) { |
265
|
1 |
|
$item[$return_type][$k] = $v[0]; |
266
|
1 |
|
} |
267
|
1 |
|
} |
268
|
1 |
|
$item[$return_type]['_id'] = $item['_id']; |
269
|
1 |
|
$result[] = $item[$return_type]; |
270
|
1 |
|
} |
271
|
1 |
|
return ['total' => $count['count'], 'data' => $result]; |
272
|
1 |
|
} |
273
|
|
|
|
274
|
1 |
|
public function query(string $collection) |
275
|
1 |
|
{ |
276
|
|
|
return new ElasticSearchQueryBuilder($collection); |
|
|
|
|
277
|
|
|
} |
278
|
1 |
|
|
279
|
|
|
public static function buildFilter(array $filter) |
280
|
1 |
|
{ |
281
|
1 |
|
$filters = []; |
282
|
1 |
|
foreach ($filter as $key => $value) { |
283
|
1 |
|
$isNot = ''; |
284
|
|
|
if (strpos($key, '__')!==false) { |
285
|
|
|
$tmpFilters = self::buildFilterForKeys($key, $value, $isNot); |
286
|
|
|
$filters = self::mergeFilters($filters, $tmpFilters); |
287
|
1 |
|
} elseif ((strpos($key, '__') === false) && is_array($value)) { |
288
|
1 |
|
$filters['should'] = self::buildFilterForOR($value); |
289
|
1 |
|
} else { |
290
|
|
|
$filters['must'][] = ['term' => [$key => $value]]; |
291
|
1 |
|
} |
292
|
1 |
|
} |
293
|
1 |
|
return $filters; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
private static function mergeFilters (array $filters, array $tmpFilters){ |
297
|
|
|
foreach ($tmpFilters as $fKey => $fVals) { |
298
|
|
|
if (isset($filters[$fKey])) { |
299
|
|
|
foreach ($fVals as $fVal) { |
300
|
|
|
$filters[$fKey][] = $fVal; |
301
|
|
|
} |
302
|
|
|
} else { |
303
|
|
|
$filters[$fKey] = $fVals; |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
return $filters; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
private static function buildFilterForKeys(string $key, $value, string $isNot) |
310
|
|
|
{ |
311
|
|
|
$filters = []; |
312
|
1 |
|
preg_match('/__(.*?)$/', $key, $matches); |
313
|
1 |
|
$operator = $matches[1]; |
314
|
1 |
|
if (strpos($operator, '!')===0) { |
315
|
|
|
$operator = str_replace('!', '', $operator); |
316
|
|
|
$isNot = '_not'; |
317
|
1 |
|
} |
318
|
|
|
$key = str_replace($matches[0], '', $key); |
319
|
1 |
|
foreach (self::$operators as $type => $possibilities) { |
320
|
1 |
|
if (in_array($operator, $possibilities, true)) { |
321
|
1 |
|
switch ($type) { |
322
|
1 |
|
case 'range': |
|
|
|
|
323
|
1 |
|
$filters['must'.$isNot][] = ['range' => [$key => [$operator => $value]]]; |
324
|
1 |
|
break; |
325
|
1 |
|
case 'standard': |
|
|
|
|
326
|
1 |
|
$filters['must'.$isNot][] = [$type => [$key => $value]]; |
327
|
|
|
break; |
328
|
|
|
case 'BooleanQueryLevel': |
329
|
1 |
|
switch ($operator) { |
330
|
1 |
|
case 'not': |
331
|
1 |
|
$filters['must_not'][] = ['term' => [$key => $value]]; |
332
|
|
|
break; |
333
|
1 |
|
} |
334
|
1 |
|
break; |
335
|
1 |
|
case 'special': |
336
|
|
|
switch ($operator) { |
337
|
|
|
case 'in': |
338
|
|
|
$filters['must'.$isNot][] = ['terms' => [$key => $value]]; |
339
|
|
|
break; |
340
|
|
|
} |
341
|
|
|
break; |
342
|
|
|
} |
343
|
|
|
} |
344
|
|
|
} |
345
|
|
|
return $filters; |
346
|
|
|
} |
347
|
1 |
|
|
348
|
1 |
|
private static function buildFilterForOR(array $orValues) |
349
|
1 |
|
{ |
350
|
|
|
$filters = []; |
351
|
|
|
foreach ($orValues as $orValue) { |
352
|
1 |
|
$subKey = array_keys($orValue)[0]; |
353
|
1 |
|
$subValue = $orValue[$subKey]; |
354
|
|
|
if (strpos($subKey, '__') !== false) { |
355
|
|
|
preg_match('/__(.*?)$/', $subKey, $subMatches); |
356
|
|
|
$subOperator = $subMatches[1]; |
357
|
|
|
if (strpos($subOperator, '!')===0) { |
358
|
|
|
$subOperator = str_replace('!', '', $subOperator); |
359
|
|
|
} |
360
|
|
|
$subKey = str_replace($subMatches[0], '', $subKey); |
361
|
|
|
foreach (self::$operators as $type => $possibilities) { |
362
|
|
|
if (in_array($subOperator, $possibilities, true)) { |
363
|
|
|
switch ($type) { |
364
|
|
|
case 'range': |
365
|
|
|
$filters[] = ['range' => [$subKey => [$subOperator => $subValue]]]; |
366
|
|
|
break; |
367
|
|
|
case 'standard': |
368
|
|
|
$filters[] = [$type => [$subKey => $subValue]]; |
369
|
|
|
break; |
370
|
|
|
case 'special': |
371
|
|
|
switch ($subOperator) { |
372
|
|
|
case 'in': |
373
|
|
|
$filters[] = ['terms' => [$subKey => $subValue]]; |
374
|
|
|
break; |
375
|
|
|
} |
376
|
|
|
break; |
377
|
|
|
} |
378
|
|
|
} |
379
|
|
|
} |
380
|
|
|
} else { |
381
|
|
|
$filters[] = ['term' => [$subKey => $subValue]]; |
382
|
|
|
} |
383
|
|
|
} |
384
|
|
|
return $filters; |
385
|
|
|
} |
386
|
|
|
} |
387
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.