1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Longman\LaravelLodash\Elasticsearch; |
6
|
|
|
|
7
|
|
|
use Elasticsearch\Client; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
|
10
|
|
|
use function array_keys; |
11
|
|
|
use function implode; |
12
|
|
|
use function reset; |
13
|
|
|
|
14
|
|
|
class ElasticsearchManager implements ElasticsearchManagerContract |
15
|
|
|
{ |
16
|
|
|
/** @var \Elasticsearch\Client */ |
17
|
|
|
protected $client; |
18
|
|
|
|
19
|
|
|
/** @var bool */ |
20
|
|
|
protected $enabled; |
21
|
|
|
|
22
|
|
|
/** @var int|null */ |
23
|
|
|
protected $timeout; |
24
|
|
|
|
25
|
|
|
public function __construct(Client $client, bool $enabled = false) |
26
|
|
|
{ |
27
|
|
|
$this->client = $client; |
28
|
|
|
$this->enabled = $enabled; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function setEnabled(bool $enabled): ElasticsearchManagerContract |
32
|
|
|
{ |
33
|
|
|
$this->enabled = $enabled; |
34
|
|
|
|
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function setTimeout(int $timeout): ElasticsearchManagerContract |
39
|
|
|
{ |
40
|
|
|
$this->timeout = $timeout; |
41
|
|
|
|
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function createIndex(string $indexName, array $settings, array $mappings): void |
46
|
|
|
{ |
47
|
|
|
if (! $this->isEnabled()) { |
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$params = [ |
52
|
|
|
'index' => $indexName, |
53
|
|
|
'body' => [ |
54
|
|
|
'settings' => $settings, |
55
|
|
|
'mappings' => $mappings, |
56
|
|
|
], |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
if (! empty($this->timeout)) { |
60
|
|
|
$params['client'] = [ |
61
|
|
|
'timeout' => $this->timeout, |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$response = $this->client->indices()->create($params); |
66
|
|
|
|
67
|
|
|
if ($response['acknowledged'] !== true) { |
68
|
|
|
throw new ElasticsearchException('Something went wrong during index creation'); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function deleteIndexes(array $names): void |
73
|
|
|
{ |
74
|
|
|
if (! $this->isEnabled()) { |
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
if (empty($names)) { |
78
|
|
|
throw new InvalidArgumentException('Index names can not be empty'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$params = [ |
82
|
|
|
'index' => implode(',', $names), |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
if (! empty($this->timeout)) { |
86
|
|
|
$params['client'] = [ |
87
|
|
|
'timeout' => $this->timeout, |
88
|
|
|
]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$response = $this->client->indices()->delete($params); |
92
|
|
|
if ($response['acknowledged'] !== true) { |
93
|
|
|
throw new ElasticsearchException('Something went wrong during index deletion'); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function deleteIndexesByAlias(string $aliasName): void |
98
|
|
|
{ |
99
|
|
|
if (! $this->isEnabled()) { |
100
|
|
|
return; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$params = [ |
104
|
|
|
'name' => $aliasName, |
105
|
|
|
]; |
106
|
|
|
|
107
|
|
|
$response = $this->client->indices()->getAlias($params); |
108
|
|
|
if (empty($response)) { |
109
|
|
|
throw new ElasticsearchException('Can not get alias ' . $aliasName); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$indexes = array_keys($response); |
113
|
|
|
$this->deleteIndexes($indexes); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @throws \Longman\LaravelLodash\Elasticsearch\ElasticsearchException |
118
|
|
|
*/ |
119
|
|
View Code Duplication |
public function addDocumentsToIndex(string $indexName, string $typeName, array $items) |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
if (! $this->isEnabled()) { |
122
|
|
|
return; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$params = [ |
126
|
|
|
'body' => [], |
127
|
|
|
]; |
128
|
|
|
|
129
|
|
|
if (! empty($this->timeout)) { |
130
|
|
|
$params['client'] = [ |
131
|
|
|
'timeout' => $this->timeout, |
132
|
|
|
]; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
foreach ($items as $id => $item) { |
136
|
|
|
$params['body'][] = [ |
137
|
|
|
'create' => [ |
138
|
|
|
'_index' => $indexName, |
139
|
|
|
'_type' => $typeName, |
140
|
|
|
'_id' => $id, |
141
|
|
|
], |
142
|
|
|
]; |
143
|
|
|
|
144
|
|
|
$params['body'][] = $item; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$responses = $this->client->bulk($params); |
148
|
|
|
if ($responses['errors'] !== true) { |
149
|
|
|
return; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$this->handleBulkError($responses, 'Error occurred during bulk create'); |
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @throws \Longman\LaravelLodash\Elasticsearch\ElasticsearchException |
157
|
|
|
*/ |
158
|
|
View Code Duplication |
public function updateDocumentsInIndex(string $indexName, string $typeName, array $items) |
|
|
|
|
159
|
|
|
{ |
160
|
|
|
if (! $this->isEnabled()) { |
161
|
|
|
return; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$params = [ |
165
|
|
|
'body' => [], |
166
|
|
|
]; |
167
|
|
|
|
168
|
|
|
if (! empty($this->timeout)) { |
169
|
|
|
$params['client'] = [ |
170
|
|
|
'timeout' => $this->timeout, |
171
|
|
|
]; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
foreach ($items as $id => $item) { |
175
|
|
|
$params['body'][] = [ |
176
|
|
|
'update' => [ |
177
|
|
|
'_index' => $indexName, |
178
|
|
|
'_type' => $typeName, |
179
|
|
|
'_id' => $id, |
180
|
|
|
], |
181
|
|
|
]; |
182
|
|
|
|
183
|
|
|
$params['body'][] = ['doc' => $item]; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$responses = $this->client->bulk($params); |
187
|
|
|
if ($responses['errors'] !== true) { |
188
|
|
|
return; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$this->handleBulkError($responses, 'Error occurred during bulk update'); |
|
|
|
|
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @throws \Longman\LaravelLodash\Elasticsearch\ElasticsearchException |
196
|
|
|
*/ |
197
|
|
View Code Duplication |
public function addOrUpdateDocumentsInIndex(string $indexName, string $typeName, array $items) |
|
|
|
|
198
|
|
|
{ |
199
|
|
|
if (! $this->isEnabled()) { |
200
|
|
|
return; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$params = [ |
204
|
|
|
'body' => [], |
205
|
|
|
]; |
206
|
|
|
|
207
|
|
|
if (! empty($this->timeout)) { |
208
|
|
|
$params['client'] = [ |
209
|
|
|
'timeout' => $this->timeout, |
210
|
|
|
]; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
foreach ($items as $id => $item) { |
214
|
|
|
$params['body'][] = [ |
215
|
|
|
'index' => [ |
216
|
|
|
'_index' => $indexName, |
217
|
|
|
'_type' => $typeName, |
218
|
|
|
'_id' => $id, |
219
|
|
|
], |
220
|
|
|
]; |
221
|
|
|
|
222
|
|
|
$params['body'][] = $item; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
$responses = $this->client->bulk($params); |
226
|
|
|
if ($responses['errors'] !== true) { |
227
|
|
|
return; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
$this->handleBulkError($responses, 'Error occurred during bulk index'); |
|
|
|
|
231
|
|
|
} |
232
|
|
|
|
233
|
|
View Code Duplication |
public function refreshIndex(string $indexName): void |
|
|
|
|
234
|
|
|
{ |
235
|
|
|
if (! $this->isEnabled()) { |
236
|
|
|
return; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
$params = [ |
240
|
|
|
'index' => $indexName, |
241
|
|
|
]; |
242
|
|
|
|
243
|
|
|
if (! empty($this->timeout)) { |
244
|
|
|
$params['client'] = [ |
245
|
|
|
'timeout' => $this->timeout, |
246
|
|
|
]; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
$this->client->indices()->refresh($params); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
View Code Duplication |
public function performSearch(ElasticsearchQueryContract $query): array |
|
|
|
|
253
|
|
|
{ |
254
|
|
|
if (! $this->isEnabled()) { |
255
|
|
|
return []; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
$params = $query->build(); |
259
|
|
|
if (! empty($this->timeout)) { |
260
|
|
|
$params['client'] = [ |
261
|
|
|
'timeout' => $this->timeout, |
262
|
|
|
]; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
$results = $this->client->search($params); |
266
|
|
|
|
267
|
|
|
return $results; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
public function switchIndexAlias(string $aliasName, string $indexName): void |
271
|
|
|
{ |
272
|
|
|
if (! $this->isEnabled()) { |
273
|
|
|
return; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
$params = [ |
277
|
|
|
'name' => $aliasName, |
278
|
|
|
]; |
279
|
|
|
|
280
|
|
|
$exists = $this->client->indices()->existsAlias($params); |
281
|
|
|
|
282
|
|
|
$actions = []; |
283
|
|
|
// If alias already exists remove from indexes |
284
|
|
|
if ($exists) { |
285
|
|
|
$params = [ |
286
|
|
|
'name' => $aliasName, |
287
|
|
|
]; |
288
|
|
|
|
289
|
|
|
$response = $this->client->indices()->getAlias($params); |
290
|
|
|
if (empty($response)) { |
291
|
|
|
throw new ElasticsearchException('Can not get alias ' . $aliasName); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
$indexes = array_keys($response); |
295
|
|
|
|
296
|
|
|
foreach ($indexes as $index) { |
297
|
|
|
$actions[] = [ |
298
|
|
|
'remove' => [ |
299
|
|
|
'index' => $index, |
300
|
|
|
'alias' => $aliasName, |
301
|
|
|
], |
302
|
|
|
]; |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
$actions[] = [ |
307
|
|
|
'add' => [ |
308
|
|
|
'index' => $indexName, |
309
|
|
|
'alias' => $aliasName, |
310
|
|
|
], |
311
|
|
|
]; |
312
|
|
|
|
313
|
|
|
$params = [ |
314
|
|
|
'body' => [ |
315
|
|
|
'actions' => $actions, |
316
|
|
|
], |
317
|
|
|
]; |
318
|
|
|
|
319
|
|
|
$response = $this->client->indices()->updateAliases($params); |
320
|
|
|
if ($response['acknowledged'] !== true) { |
321
|
|
|
throw new ElasticsearchException('Switching alias response error'); |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
public function createTemplate(string $name, array $settings): void |
326
|
|
|
{ |
327
|
|
|
if (! $this->isEnabled()) { |
328
|
|
|
return; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
$params = [ |
332
|
|
|
'name' => $name, |
333
|
|
|
'body' => $settings, |
334
|
|
|
]; |
335
|
|
|
|
336
|
|
|
if (! empty($this->timeout)) { |
337
|
|
|
$params['client'] = [ |
338
|
|
|
'timeout' => $this->timeout, |
339
|
|
|
]; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
$response = $this->client->indices()->putTemplate($params); |
343
|
|
|
|
344
|
|
|
if ($response['acknowledged'] !== true) { |
345
|
|
|
throw new ElasticsearchException('Something went wrong during template creation'); |
346
|
|
|
} |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
public function ping(): bool |
350
|
|
|
{ |
351
|
|
|
if (! $this->isEnabled()) { |
352
|
|
|
return false; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
return $this->client->ping(); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
public function getClient(): Client |
359
|
|
|
{ |
360
|
|
|
return $this->client; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
public function isEnabled(): bool |
364
|
|
|
{ |
365
|
|
|
return $this->enabled; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* @throws \Longman\LaravelLodash\Elasticsearch\ElasticsearchException |
370
|
|
|
*/ |
371
|
|
|
protected function handleBulkError(array $responses, string $message) |
372
|
|
|
{ |
373
|
|
|
$errors = []; |
374
|
|
|
foreach ($responses['items'] as $item) { |
375
|
|
|
$row = $item; |
376
|
|
|
$row = reset($row); |
377
|
|
|
if (empty($row['error'])) { |
378
|
|
|
continue; |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
$errors[] = $row['error']; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
throw new ElasticsearchException($message, $errors); |
385
|
|
|
} |
386
|
|
|
} |
387
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.