@@ -33,9 +33,9 @@ class Elasticsearch implements ClientInterface
33
33
private array $ client ;
34
34
35
35
/**
36
- * @var bool|null
36
+ * @var bool
37
37
*/
38
- private ? bool $ pingResult = null ;
38
+ private bool $ pingResult = false ;
39
39
40
40
/**
41
41
* @var FieldsMappingPreprocessorInterface[]
@@ -96,9 +96,7 @@ private function getElasticsearchClient(): ?Client /** @phpstan-ignore-line */
96
96
$ pid = getmypid ();
97
97
if (!isset ($ this ->client [$ pid ])) {
98
98
$ config = $ this ->buildESConfig ($ this ->clientOptions );
99
- if (class_exists (\Elastic \Elasticsearch \ClientBuilder::class)) {
100
- $ this ->client [$ pid ] = ClientBuilder::fromConfig ($ config , true ); /** @phpstan-ignore-line */
101
- }
99
+ $ this ->client [$ pid ] = ClientBuilder::fromConfig ($ config , true ); /** @phpstan-ignore-line */
102
100
}
103
101
104
102
return $ this ->client [$ pid ];
@@ -111,9 +109,11 @@ private function getElasticsearchClient(): ?Client /** @phpstan-ignore-line */
111
109
*/
112
110
public function ping (): bool
113
111
{
114
- if ($ this ->pingResult === null ) {
115
- $ this ->pingResult = $ this ->getElasticsearchClient ()
116
- ->ping (['client ' => ['timeout ' => $ this ->clientOptions ['timeout ' ]]])->asBool ();
112
+ $ elasticsearchClient = $ this ->getElasticsearchClient ();
113
+ if ($ this ->pingResult === false && $ elasticsearchClient ) {
114
+ $ this ->pingResult = $ elasticsearchClient ->ping (
115
+ ['client ' => ['timeout ' => $ this ->clientOptions ['timeout ' ]]]
116
+ )->asBool ();
117
117
}
118
118
119
119
return $ this ->pingResult ;
@@ -138,12 +138,11 @@ public function testConnection(): bool
138
138
*/
139
139
public function putIndexSettings (string $ index , array $ settings ): void
140
140
{
141
- $ this ->getElasticsearchClient ()->indices ()->putSettings (
142
- [
143
- 'index ' => $ index ,
144
- 'body ' => $ settings ,
145
- ]
146
- );
141
+ $ elasticsearchClient = $ this ->getElasticsearchClient ();
142
+ if ($ elasticsearchClient ) {
143
+ $ elasticsearchClient ->indices ()
144
+ ->putSettings (['index ' => $ index , 'body ' => $ settings ]);
145
+ }
147
146
}
148
147
149
148
/**
@@ -156,6 +155,11 @@ public function putIndexSettings(string $index, array $settings): void
156
155
*/
157
156
public function updateAlias (string $ alias , string $ newIndex , string $ oldIndex = '' )
158
157
{
158
+ $ elasticsearchClient = $ this ->getElasticsearchClient ();
159
+ if ($ elasticsearchClient === null ) {
160
+ return ;
161
+ }
162
+
159
163
$ params = ['body ' => ['actions ' => []]];
160
164
if ($ newIndex ) {
161
165
$ params ['body ' ]['actions ' ][] = ['add ' => ['alias ' => $ alias , 'index ' => $ newIndex ]];
@@ -165,7 +169,7 @@ public function updateAlias(string $alias, string $newIndex, string $oldIndex =
165
169
$ params ['body ' ]['actions ' ][] = ['remove ' => ['alias ' => $ alias , 'index ' => $ oldIndex ]];
166
170
}
167
171
168
- $ this -> getElasticsearchClient () ->indices ()->updateAliases ($ params );
172
+ $ elasticsearchClient ->indices ()->updateAliases ($ params );
169
173
}
170
174
171
175
/**
@@ -176,7 +180,15 @@ public function updateAlias(string $alias, string $newIndex, string $oldIndex =
176
180
*/
177
181
public function indexExists (string $ index ): bool
178
182
{
179
- return $ this ->getElasticsearchClient ()->indices ()->exists (['index ' => $ index ])->asBool ();
183
+ $ indexExists = false ;
184
+ $ elasticsearchClient = $ this ->getElasticsearchClient ();
185
+ if ($ elasticsearchClient ) {
186
+ $ indexExists = $ elasticsearchClient ->indices ()
187
+ ->exists (['index ' => $ index ])
188
+ ->asBool ();
189
+ }
190
+
191
+ return $ indexExists ;
180
192
}
181
193
182
194
/**
@@ -217,15 +229,22 @@ private function buildESConfig(array $options = []): array
217
229
*
218
230
* @param string $alias
219
231
* @param string $index
232
+ * @return bool
220
233
*/
221
- public function existsAlias (string $ alias , string $ index = '' )
234
+ public function existsAlias (string $ alias , string $ index = '' ): bool
222
235
{
223
- $ params = ['name ' => $ alias ];
224
- if ($ index ) {
225
- $ params ['index ' ] = $ index ;
236
+ $ existAlias = false ;
237
+ $ elasticsearchClient = $ this ->getElasticsearchClient ();
238
+ if ($ elasticsearchClient ) {
239
+ $ params = ['name ' => $ alias ];
240
+ if ($ index ) {
241
+ $ params ['index ' ] = $ index ;
242
+ }
243
+
244
+ $ existAlias = $ elasticsearchClient ->indices ()->existsAlias ($ params )->asBool ();
226
245
}
227
246
228
- return $ this -> getElasticsearchClient ()-> indices ()-> existsAlias ( $ params )-> asBool () ;
247
+ return $ existAlias ;
229
248
}
230
249
231
250
/**
@@ -236,7 +255,10 @@ public function existsAlias(string $alias, string $index = '')
236
255
*/
237
256
public function bulkQuery (array $ query )
238
257
{
239
- $ this ->getElasticsearchClient ()->bulk ($ query );
258
+ $ elasticsearchClient = $ this ->getElasticsearchClient ();
259
+ if ($ elasticsearchClient ) {
260
+ $ elasticsearchClient ->bulk ($ query );
261
+ }
240
262
}
241
263
242
264
/**
@@ -246,14 +268,16 @@ public function bulkQuery(array $query)
246
268
* @param array $settings
247
269
* @return void
248
270
*/
249
- public function createIndex (string $ index , array $ settings )
271
+ public function createIndex (string $ index , array $ settings ): void
250
272
{
251
- $ this ->getElasticsearchClient ()->indices ()->create (
252
- [
253
- 'index ' => $ index ,
254
- 'body ' => $ settings ,
255
- ]
256
- );
273
+ $ elasticsearchClient = $ this ->getElasticsearchClient ();
274
+ if ($ elasticsearchClient ) {
275
+ $ elasticsearchClient ->indices ()
276
+ ->create ([
277
+ 'index ' => $ index ,
278
+ 'body ' => $ settings ,
279
+ ]);
280
+ }
257
281
}
258
282
259
283
/**
@@ -264,7 +288,14 @@ public function createIndex(string $index, array $settings)
264
288
*/
265
289
public function getAlias (string $ alias ): array
266
290
{
267
- return $ this ->getElasticsearchClient ()->indices ()->getAlias (['name ' => $ alias ])->asArray ();
291
+ $ elasticsearchClient = $ this ->getElasticsearchClient ();
292
+ if ($ elasticsearchClient === null ) {
293
+ return [];
294
+ }
295
+
296
+ return $ elasticsearchClient ->indices ()
297
+ ->getAlias (['name ' => $ alias ])
298
+ ->asArray ();
268
299
}
269
300
270
301
/**
@@ -278,6 +309,11 @@ public function getAlias(string $alias): array
278
309
*/
279
310
public function addFieldsMapping (array $ fields , string $ index , string $ entityType )
280
311
{
312
+ $ elasticsearchClient = $ this ->getElasticsearchClient ();
313
+ if ($ elasticsearchClient === null ) {
314
+ return ;
315
+ }
316
+
281
317
$ params = [
282
318
'index ' => $ index ,
283
319
'body ' => [
@@ -290,7 +326,7 @@ public function addFieldsMapping(array $fields, string $index, string $entityTyp
290
326
$ params ['body ' ]['properties ' ][$ field ] = $ fieldInfo ;
291
327
}
292
328
293
- $ this -> getElasticsearchClient () ->indices ()->putMapping ($ params );
329
+ $ elasticsearchClient ->indices ()->putMapping ($ params );
294
330
}
295
331
296
332
/**
@@ -301,7 +337,11 @@ public function addFieldsMapping(array $fields, string $index, string $entityTyp
301
337
*/
302
338
public function deleteIndex (string $ index )
303
339
{
304
- $ this ->getElasticsearchClient ()->indices ()->delete (['index ' => $ index ]);
340
+ $ elasticsearchClient = $ this ->getElasticsearchClient ();
341
+ if ($ elasticsearchClient ) {
342
+ $ elasticsearchClient ->indices ()
343
+ ->delete (['index ' => $ index ]);
344
+ }
305
345
}
306
346
307
347
/**
@@ -312,6 +352,11 @@ public function deleteIndex(string $index)
312
352
*/
313
353
public function isEmptyIndex (string $ index ): bool
314
354
{
355
+ $ elasticsearchClient = $ this ->getElasticsearchClient ();
356
+ if ($ elasticsearchClient === null ) {
357
+ return false ;
358
+ }
359
+
315
360
$ stats = $ this ->getElasticsearchClient ()->indices ()->stats (['index ' => $ index , 'metric ' => 'docs ' ]);
316
361
if ($ stats ['indices ' ][$ index ]['primaries ' ]['docs ' ]['count ' ] === 0 ) {
317
362
return true ;
@@ -327,7 +372,9 @@ public function isEmptyIndex(string $index): bool
327
372
*/
328
373
public function query (array $ query ): array
329
374
{
330
- return $ this ->getElasticsearchClient ()->search ($ query )->asArray ();
375
+ $ elasticsearchClient = $ this ->getElasticsearchClient ();
376
+
377
+ return $ elasticsearchClient === null ? [] : $ elasticsearchClient ->search ($ query )->asArray ();
331
378
}
332
379
333
380
/**
@@ -338,7 +385,9 @@ public function query(array $query): array
338
385
*/
339
386
public function getMapping (array $ params ): array
340
387
{
341
- return $ this ->getElasticsearchClient ()->indices ()->getMapping ($ params )->asArray ();
388
+ $ elasticsearchClient = $ this ->getElasticsearchClient ();
389
+
390
+ return $ elasticsearchClient === null ? [] : $ elasticsearchClient ->indices ()->getMapping ($ params )->asArray ();
342
391
}
343
392
344
393
/**
0 commit comments