Skip to content

Commit 1b688d4

Browse files
committed
AC-7116: Fixed DBC test failures
1 parent e8d2d3a commit 1b688d4

File tree

1 file changed

+83
-34
lines changed

1 file changed

+83
-34
lines changed

app/code/Magento/Elasticsearch8/Model/Client/Elasticsearch.php

Lines changed: 83 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ class Elasticsearch implements ClientInterface
3333
private array $client;
3434

3535
/**
36-
* @var bool|null
36+
* @var bool
3737
*/
38-
private ?bool $pingResult = null;
38+
private bool $pingResult = false;
3939

4040
/**
4141
* @var FieldsMappingPreprocessorInterface[]
@@ -96,9 +96,7 @@ private function getElasticsearchClient(): ?Client /** @phpstan-ignore-line */
9696
$pid = getmypid();
9797
if (!isset($this->client[$pid])) {
9898
$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 */
102100
}
103101

104102
return $this->client[$pid];
@@ -111,9 +109,11 @@ private function getElasticsearchClient(): ?Client /** @phpstan-ignore-line */
111109
*/
112110
public function ping(): bool
113111
{
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();
117117
}
118118

119119
return $this->pingResult;
@@ -138,12 +138,11 @@ public function testConnection(): bool
138138
*/
139139
public function putIndexSettings(string $index, array $settings): void
140140
{
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+
}
147146
}
148147

149148
/**
@@ -156,6 +155,11 @@ public function putIndexSettings(string $index, array $settings): void
156155
*/
157156
public function updateAlias(string $alias, string $newIndex, string $oldIndex = '')
158157
{
158+
$elasticsearchClient = $this->getElasticsearchClient();
159+
if ($elasticsearchClient === null) {
160+
return;
161+
}
162+
159163
$params = ['body' => ['actions' => []]];
160164
if ($newIndex) {
161165
$params['body']['actions'][] = ['add' => ['alias' => $alias, 'index' => $newIndex]];
@@ -165,7 +169,7 @@ public function updateAlias(string $alias, string $newIndex, string $oldIndex =
165169
$params['body']['actions'][] = ['remove' => ['alias' => $alias, 'index' => $oldIndex]];
166170
}
167171

168-
$this->getElasticsearchClient()->indices()->updateAliases($params);
172+
$elasticsearchClient->indices()->updateAliases($params);
169173
}
170174

171175
/**
@@ -176,7 +180,15 @@ public function updateAlias(string $alias, string $newIndex, string $oldIndex =
176180
*/
177181
public function indexExists(string $index): bool
178182
{
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;
180192
}
181193

182194
/**
@@ -217,15 +229,22 @@ private function buildESConfig(array $options = []): array
217229
*
218230
* @param string $alias
219231
* @param string $index
232+
* @return bool
220233
*/
221-
public function existsAlias(string $alias, string $index = '')
234+
public function existsAlias(string $alias, string $index = ''): bool
222235
{
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();
226245
}
227246

228-
return $this->getElasticsearchClient()->indices()->existsAlias($params)->asBool();
247+
return $existAlias;
229248
}
230249

231250
/**
@@ -236,7 +255,10 @@ public function existsAlias(string $alias, string $index = '')
236255
*/
237256
public function bulkQuery(array $query)
238257
{
239-
$this->getElasticsearchClient()->bulk($query);
258+
$elasticsearchClient = $this->getElasticsearchClient();
259+
if ($elasticsearchClient) {
260+
$elasticsearchClient->bulk($query);
261+
}
240262
}
241263

242264
/**
@@ -246,14 +268,16 @@ public function bulkQuery(array $query)
246268
* @param array $settings
247269
* @return void
248270
*/
249-
public function createIndex(string $index, array $settings)
271+
public function createIndex(string $index, array $settings): void
250272
{
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+
}
257281
}
258282

259283
/**
@@ -264,7 +288,14 @@ public function createIndex(string $index, array $settings)
264288
*/
265289
public function getAlias(string $alias): array
266290
{
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();
268299
}
269300

270301
/**
@@ -278,6 +309,11 @@ public function getAlias(string $alias): array
278309
*/
279310
public function addFieldsMapping(array $fields, string $index, string $entityType)
280311
{
312+
$elasticsearchClient = $this->getElasticsearchClient();
313+
if ($elasticsearchClient === null) {
314+
return;
315+
}
316+
281317
$params = [
282318
'index' => $index,
283319
'body' => [
@@ -290,7 +326,7 @@ public function addFieldsMapping(array $fields, string $index, string $entityTyp
290326
$params['body']['properties'][$field] = $fieldInfo;
291327
}
292328

293-
$this->getElasticsearchClient()->indices()->putMapping($params);
329+
$elasticsearchClient->indices()->putMapping($params);
294330
}
295331

296332
/**
@@ -301,7 +337,11 @@ public function addFieldsMapping(array $fields, string $index, string $entityTyp
301337
*/
302338
public function deleteIndex(string $index)
303339
{
304-
$this->getElasticsearchClient()->indices()->delete(['index' => $index]);
340+
$elasticsearchClient = $this->getElasticsearchClient();
341+
if ($elasticsearchClient) {
342+
$elasticsearchClient->indices()
343+
->delete(['index' => $index]);
344+
}
305345
}
306346

307347
/**
@@ -312,6 +352,11 @@ public function deleteIndex(string $index)
312352
*/
313353
public function isEmptyIndex(string $index): bool
314354
{
355+
$elasticsearchClient = $this->getElasticsearchClient();
356+
if ($elasticsearchClient === null) {
357+
return false;
358+
}
359+
315360
$stats = $this->getElasticsearchClient()->indices()->stats(['index' => $index, 'metric' => 'docs']);
316361
if ($stats['indices'][$index]['primaries']['docs']['count'] === 0) {
317362
return true;
@@ -327,7 +372,9 @@ public function isEmptyIndex(string $index): bool
327372
*/
328373
public function query(array $query): array
329374
{
330-
return $this->getElasticsearchClient()->search($query)->asArray();
375+
$elasticsearchClient = $this->getElasticsearchClient();
376+
377+
return $elasticsearchClient === null ? [] : $elasticsearchClient->search($query)->asArray();
331378
}
332379

333380
/**
@@ -338,7 +385,9 @@ public function query(array $query): array
338385
*/
339386
public function getMapping(array $params): array
340387
{
341-
return $this->getElasticsearchClient()->indices()->getMapping($params)->asArray();
388+
$elasticsearchClient = $this->getElasticsearchClient();
389+
390+
return $elasticsearchClient === null ? [] : $elasticsearchClient->indices()->getMapping($params)->asArray();
342391
}
343392

344393
/**

0 commit comments

Comments
 (0)