-
Notifications
You must be signed in to change notification settings - Fork 7
/
ElasticConnection.php
executable file
·498 lines (396 loc) · 15 KB
/
ElasticConnection.php
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
<?php
namespace DoctrineElastic\Connection;
use DoctrineElastic\Exception\ConnectionException;
use DoctrineElastic\Exception\ElasticOperationException;
use DoctrineElastic\Exception\InvalidParamsException;
use DoctrineElastic\Helper\MappingHelper;
use DoctrineElastic\Http\CurlRequest;
use DoctrineElastic\Traiting\ErrorGetterTrait;
/**
* Default elastic connection class for general operations
* Notice that the original elastic result of most of operations can be get by $return param
*
* @author Andsalves <ands.alves.nunes@gmail.com>
*/
class ElasticConnection implements ElasticConnectionInterface {
use ErrorGetterTrait;
/** Override default elastic limit size query */
const DEFAULT_MAX_RESULTS = 10000;
/** @var CurlRequest */
protected $curlRequest;
/** @var float */
protected $esVersion;
public function __construct(array $hosts) {
$this->curlRequest = new CurlRequest();
$baseHost = reset($hosts);
if (empty($baseHost) || !is_string($baseHost) || !preg_match('/http/', $baseHost)) {
throw new ConnectionException("Elasticsearch host is invalid. ");
}
$this->curlRequest->setBaseUrl($baseHost);
}
/**
* @param string $index
* @param array|null $mappings
* @param array|null $settings
* @param array|null $aliases
* @param array|null $return
* @return bool
*/
public function createIndex(
$index, array $mappings = null, array $settings = null, array $aliases = null, array &$return = null
) {
if ($this->indexExists($index)) {
throw new \InvalidArgumentException(sprintf("'%s' index already exists", $index));
}
$params = [];
if (is_array($mappings) && !empty($mappings)) {
$params['mappings'] = MappingHelper::patchMappings($mappings, floor($this->getElasticsearchVersion()));
}
if (is_array($settings) && !empty($settings)) {
$params['settings'] = $settings;
}
if (is_array($aliases) && !empty($aliases)) {
$params['aliases'] = $aliases;
}
$response = $this->curlRequest->request($index, $params, 'PUT');
$return = $response['content'];
if (isset($return['acknowledged']) && $return['acknowledged']) {
return $return['acknowledged'];
}
$this->setErrorFromElasticReturn($return);
return false;
}
/**
* @param string $index
* @param array|null $return
* @return bool
* @throws ElasticOperationException
*/
public function deleteIndex($index, array &$return = null) {
if (is_string($index) && !strstr('_all', $index) && !strstr('*', $index)) {
$response = $this->curlRequest->request("$index?refresh=true", [], 'DELETE');
$return = $response['content'];
if ($response['status'] == 404) {
throw new ElasticOperationException("Index '$index' doesn't exist so cannot be deleted. ");
}
if (isset($return['acknowledged'])) {
return $return['acknowledged'];
}
} else {
throw new ElasticOperationException('Index name is invalid for deletion. ');
}
$this->setErrorFromElasticReturn($return);
return false;
}
/**
* @param string $index
* @param string $type
* @param array $mappings
* @param array|null $return
* @return bool
* @throws ElasticOperationException
*/
public function createType($index, $type, array $mappings = [], array &$return = null) {
if (!$this->indexExists($index)) {
throw new \InvalidArgumentException(sprintf("%s' index does not exists", $index));
}
if ($this->typeExists($index, $type)) {
throw new \InvalidArgumentException(sprintf("Type 's%' already exists on index %s", $type, $index));
}
$mappings = MappingHelper::patchMappings($mappings, floor($this->getElasticsearchVersion()));
$url = "$index/_mapping/$type";
$response = $this->curlRequest->request($url, $mappings, 'PUT');
$this->throwExceptionFromResponse($response, "Error creating type '$type' in '$index' index");
$return = $response['content'];
if (isset($return['acknowledged'])) {
return $return['acknowledged'];
}
$this->setErrorFromElasticReturn($return);
return false;
}
/**
* @param string $index
* @param string $type
* @param array $body
* @param array $queryParams
* @param array|null $return
* @return bool
*/
public function insert($index, $type, array $body, array $queryParams = [], array &$return = null) {
$url = "$index/$type";
if (isset($body['_id'])) {
$url .= '/' . $body['_id'];
unset($body['_id']);
}
$url = "$url?" . http_build_query(array_merge(['refresh' => "true"], $queryParams));
$response = $this->curlRequest->request($url, $body, 'POST');
$this->throwExceptionFromResponse($response);
$return = $response['content'];
if (isset($return['created'])) {
return $return['created'];
}
$this->setErrorFromElasticReturn($return);
return false;
}
/**
* @param string $index
* @param string $type
* @param string $_id
* @param array $body
* @param array $queryParams
* @param array|null $return
*
* @return bool
*/
public function update($index, $type, $_id, array $body = [], array $queryParams = [], array &$return = null) {
if (!$this->indexExists($index)) {
return false;
}
if (array_key_exists('doc', $body)) {
$params = $body;
} else {
$params = ['doc' => $body];
}
$url = "$index/$type/$_id/_update?" . http_build_query(array_merge(['refresh' => 'true'], $queryParams));
$response = $this->curlRequest->request($url, $params, 'POST');
$this->throwExceptionFromResponse($response);
$return = $response['content'];
if (isset($return['_id'])) {
return true;
}
$this->setErrorFromElasticReturn($return);
return false;
}
/**
* @param string $index
* @param string $type
* @param string $_id
* @param array $queryParams
* @param array|null $return
* @return bool
*/
public function delete($index, $type, $_id, array $queryParams = [], array &$return = null) {
if (!$this->indexExists($index)) {
return false;
}
$url = "$index/$type/$_id?" . http_build_query(array_merge(['refresh' => 'true'], $queryParams));
$response = $this->curlRequest->request($url, [], 'DELETE');
$this->throwExceptionFromResponse($response);
$return = $response['content'];
if (isset($return['found']) && !$return['found']) {
error_log("Doc with _id '$_id' was not found for delete. Index: '$index', Type: '$type' ");
}
if (isset($return['_id'])) {
return true;
}
$this->setErrorFromElasticReturn($return);
return false;
}
public function updateWhere($index, $type, array $where, array &$return = null) {
// TODO
}
public function deleteWhere($index, $type, array $where, array &$return = null) {
// TODO
}
/**
*
* @param string $index
* @param string $type
* @param string $_id
* @param array $queryParams
* @param array|null $return
* @return array|null
*/
public function get($index, $type, $_id, array $queryParams = [], array &$return = null) {
if (!$this->indexExists($index)) {
return null;
}
$url = "$index/$type/$_id";
$response = $this->curlRequest->request($url, [], 'GET');
$return = $response['content'];
if ($response['status'] == 404) {
return null;
}
if (isset($return['found']) && boolval($return['found'])) {
return $return;
}
return null;
}
/**
* Returns the [hits][hits] array from query
*
* @param string $index
* @param string $type
* @param array $body
* @param array $queryParams
* @param array|null $return
* @return array
*/
public function search($index, $type, array $body = [], array $queryParams = [], array &$return = null) {
if (!$this->indexExists($index)) {
return [];
}
$queryParams = array_replace_recursive([
'size' => self::DEFAULT_MAX_RESULTS
], array_filter($queryParams, function ($value) {
return $value !== null;
}));
$body = $this->unsetEmpties($body);
if (isset($body['query']) && empty($body['query'])) {
unset($body['query']);
}
$url = "$index/$type/_search?" . http_build_query($queryParams);
$cleanQuery = function ($queryPart, callable $recusiveFn) {
if (!is_array($queryPart)) {
return $queryPart;
}
foreach ($queryPart as $key => $item) {
if ($key == 'query' && isset($queryPart['query']['bool'])) {
$queryPart['bool'] = $recusiveFn($queryPart['query']['bool'], $recusiveFn);
unset($queryPart['query']['bool']);
}
if (isset($item['query']['bool'])) {
$queryPart[$key]['bool'] = $recusiveFn($item['query']['bool'], $recusiveFn);
unset($queryPart[$key]['query']);
} else if (is_array($item)) {
$queryPart[$key] = $recusiveFn($item, $recusiveFn);
}
}
return $queryPart;
};
if (isset($body['query'])) {
foreach ($body['query']['bool'] as $key => $item) {
$body['query']['bool'][$key] = $cleanQuery($item, $cleanQuery);
}
}
$response = $this->curlRequest->request($url, $body, 'POST');
$this->throwExceptionFromResponse($response);
$return = $response['content'];
if (isset($return['hits']['hits'])) {
return $return['hits']['hits'];
}
return [];
}
/**
* @param $action
* @param $index
* @param $type
* @param array|null $data
* @return array|bool
* @throws InvalidParamsException
*/
public function bulk($action, $index, $type, array $data = null) {
$bulkData = '';
if (!in_array($action, ['create', 'index', 'update']) && !is_array($data)) {
throw new InvalidParamsException(
"\$data param must be an array for '$action' bulk action"
);
}
switch ($action) {
case 'create':
case 'index':
foreach ($data as $doc) {
$actionParams = ['_index' => $index, '_type' => $type];
if (isset($doc['_id'])) {
$actionParams['_id'] = $doc['_id'];
unset($doc['_id']);
}
$bulkData .= json_encode([$action => $actionParams]) . "\n";
$bulkData .= json_encode($doc) . "\n";
}
break;
case 'update':
case 'delete':
foreach ($data as $doc) {
if (!isset($doc['_id'])) {
throw new InvalidParamsException(
"_id field must be provided for each item in \$data param for $action action"
);
}
$actionParams = ['_index' => $index, '_type' => $type, '_id' => $doc['_id']];
unset($doc['_id']);
if ($action == 'update') {
if (!array_key_exists('doc', $doc)) {
$doc = ['doc' => $doc];
}
$bulkData .= json_encode([$action => $actionParams]) . "\n";
$bulkData .= json_encode($doc) . "\n";
}
}
break;
default:
$bulkActions = ['index', 'create', 'delete', 'update'];
throw new InvalidParamsException(
"Invalid 'action' param provided. Must be one of those: " . implode('|', $bulkActions)
);
}
$response = $this->curlRequest->request("/_bulk", $bulkData, 'POST', [
'Content-Type: application/x-ndjson; charset=utf-8'
]);
if (isset($response['status']) && $response['status'] == 200) {
return $response['content'];
} elseif (isset($response['content'])) {
return $response['content'];
}
return false;
}
private function unsetEmpties(array $haystack) {
$selfFn = __FUNCTION__;
foreach ($haystack as $key => $value) {
if (is_array($value)) {
$haystack[$key] = $this->$selfFn($haystack[$key]);
}
if (is_array($haystack[$key]) && empty($haystack[$key])) {
unset($haystack[$key]);
}
}
return $haystack;
}
/**
* @param string $index
* @return bool
*/
public function indexExists($index) {
$response = $this->curlRequest->request($index, [], 'HEAD');
return $response['status'] === 200;
}
/**
* @param string $index
* @param string $type
* @return bool
*/
public function typeExists($index, $type) {
$response = $this->curlRequest->request("$index/$type", [], 'HEAD');
return $response['status'] === 200;
}
private function throwExceptionFromResponse($response, $appendPrefix = '') {
if (isset($response['content']['error']['reason'])) {
if (!empty($appendPrefix)) {
$appendPrefix .= ': ';
}
throw new ElasticOperationException($appendPrefix . $response['content']['error']['reason']);
}
}
public function hasConnection() {
$response = $this->curlRequest->request('', [], 'HEAD');
return $response['status'] == 200;
}
private function setErrorFromElasticReturn($return) {
if (isset($return['error']['root_cause'][0]['reason'])) {
$this->setError($return['error']['root_cause'][0]['reason']);
} else if (isset($return['error']['reason'])) {
$this->setError($return['error']['reason']);
}
}
public function getElasticsearchVersion() {
if (is_null($this->esVersion)) {
$response = $this->curlRequest->request('', [], 'GET');
if (isset($response['content']['version']['number'])) {
$this->esVersion = floatval($response['content']['version']['number']);
} else {
throw new ConnectionException('Unable to fetch elasticsearch version. ');
}
}
return $this->esVersion;
}
}