Skip to content

Commit ebe8018

Browse files
karyna-tandrewbess
authored andcommitted
AC-2643: Introduce OpenSearch module
1 parent 5dd75c7 commit ebe8018

File tree

64 files changed

+2076
-417
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+2076
-417
lines changed

app/code/Magento/CatalogSearch/Setup/Patch/Data/MySQLSearchRemovalNotification.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function apply(): DataPatchInterface
4848
if ($this->scopeConfig->getValue('catalog/search/engine') === 'mysql') {
4949
$message = <<<MESSAGE
5050
Catalog Search is currently configured to use the MySQL engine, which has been deprecated and removed.
51-
Migrate to an Elasticsearch engine to ensure there are no service interruptions.
51+
Migrate to an Elasticsearch/OpenSearch engine to ensure there are no service interruptions.
5252
MESSAGE;
5353

5454
$this->notifier->addNotice(__('Disable Notice'), __($message));

app/code/Magento/Config/Test/Mftf/ActionGroup/ChooseElasticSearchAsSearchEngineActionGroup.xml

Lines changed: 0 additions & 28 deletions
This file was deleted.

app/code/Magento/Elasticsearch/Model/Adapter/Elasticsearch.php

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\Elasticsearch\Model\Adapter;
88

99
use Elasticsearch\Common\Exceptions\Missing404Exception;
10+
use Exception;
1011
use Magento\AdvancedSearch\Model\Client\ClientInterface;
1112
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
1213
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\StaticField;
@@ -28,10 +29,10 @@ class Elasticsearch
2829
/**#@+
2930
* Text flags for Elasticsearch bulk actions
3031
*/
31-
const BULK_ACTION_INDEX = 'index';
32-
const BULK_ACTION_CREATE = 'create';
33-
const BULK_ACTION_DELETE = 'delete';
34-
const BULK_ACTION_UPDATE = 'update';
32+
public const BULK_ACTION_INDEX = 'index';
33+
public const BULK_ACTION_CREATE = 'create';
34+
public const BULK_ACTION_DELETE = 'delete';
35+
public const BULK_ACTION_UPDATE = 'update';
3536
/**#@-*/
3637

3738
/**
@@ -109,6 +110,13 @@ class Elasticsearch
109110
*/
110111
private $arrayManager;
111112

113+
/**
114+
* @var array
115+
*/
116+
private $responseErrorExceptionList = [
117+
'elasticsearchMissing404' => Missing404Exception::class
118+
];
119+
112120
/**
113121
* @param ConnectionManager $connectionManager
114122
* @param FieldMapperInterface $fieldMapper
@@ -121,6 +129,7 @@ class Elasticsearch
121129
* @param ProductAttributeRepositoryInterface|null $productAttributeRepository
122130
* @param StaticField|null $staticFieldProvider
123131
* @param ArrayManager|null $arrayManager
132+
* @param array $responseErrorExceptionList
124133
* @throws LocalizedException
125134
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
126135
*/
@@ -135,7 +144,8 @@ public function __construct(
135144
$options = [],
136145
ProductAttributeRepositoryInterface $productAttributeRepository = null,
137146
StaticField $staticFieldProvider = null,
138-
ArrayManager $arrayManager = null
147+
ArrayManager $arrayManager = null,
148+
array $responseErrorExceptionList = []
139149
) {
140150
$this->connectionManager = $connectionManager;
141151
$this->fieldMapper = $fieldMapper;
@@ -150,10 +160,11 @@ public function __construct(
150160
ObjectManager::getInstance()->get(StaticField::class);
151161
$this->arrayManager = $arrayManager ?:
152162
ObjectManager::getInstance()->get(ArrayManager::class);
163+
$this->responseErrorExceptionList = array_merge($this->responseErrorExceptionList, $responseErrorExceptionList);
153164

154165
try {
155166
$this->client = $this->connectionManager->getConnection($options);
156-
} catch (\Exception $e) {
167+
} catch (Exception $e) {
157168
$this->logger->critical($e);
158169
throw new LocalizedException(
159170
__('The search failed because of a search engine misconfiguration.')
@@ -171,7 +182,7 @@ public function ping()
171182
{
172183
try {
173184
$response = $this->client->ping();
174-
} catch (\Exception $e) {
185+
} catch (Exception $e) {
175186
throw new LocalizedException(
176187
__('Could not ping search engine: %1', $e->getMessage())
177188
);
@@ -205,7 +216,7 @@ public function prepareDocsPerStore(array $documentData, $storeId)
205216
* @param int $storeId
206217
* @param string $mappedIndexerId
207218
* @return $this
208-
* @throws \Exception
219+
* @throws Exception
209220
*/
210221
public function addDocs(array $documents, $storeId, $mappedIndexerId)
211222
{
@@ -214,7 +225,7 @@ public function addDocs(array $documents, $storeId, $mappedIndexerId)
214225
$indexName = $this->indexNameResolver->getIndexName($storeId, $mappedIndexerId, $this->preparedIndex);
215226
$bulkIndexDocuments = $this->getDocsArrayInBulkIndexFormat($documents, $indexName);
216227
$this->client->bulkQuery($bulkIndexDocuments);
217-
} catch (\Exception $e) {
228+
} catch (Exception $e) {
218229
$this->logger->critical($e);
219230
throw $e;
220231
}
@@ -258,7 +269,7 @@ public function cleanIndex($storeId, $mappedIndexerId)
258269
// remove index if already exists, wildcard deletion may cause collisions
259270
try {
260271
$this->client->deleteIndex($indexToDelete);
261-
} catch (\Exception $e) {
272+
} catch (Exception $e) {
262273
$this->logger->critical($e);
263274
}
264275
}
@@ -276,7 +287,7 @@ public function cleanIndex($storeId, $mappedIndexerId)
276287
* @param int $storeId
277288
* @param string $mappedIndexerId
278289
* @return $this
279-
* @throws \Exception
290+
* @throws Exception
280291
*/
281292
public function deleteDocs(array $documentIds, $storeId, $mappedIndexerId)
282293
{
@@ -289,7 +300,7 @@ public function deleteDocs(array $documentIds, $storeId, $mappedIndexerId)
289300
self::BULK_ACTION_DELETE
290301
);
291302
$this->client->bulkQuery($bulkDeleteDocuments);
292-
} catch (\Exception $e) {
303+
} catch (Exception $e) {
293304
$this->logger->critical($e);
294305
throw $e;
295306
}
@@ -391,7 +402,7 @@ public function updateAlias($storeId, $mappedIndexerId)
391402
if ($oldIndex) {
392403
try {
393404
$this->client->deleteIndex($oldIndex);
394-
} catch (\Exception $e) {
405+
} catch (Exception $e) {
395406
$this->logger->critical($e);
396407
}
397408
unset($this->indexByCode[$mappedIndexerId . '_' . $storeId]);
@@ -417,15 +428,30 @@ public function updateIndexMapping(int $storeId, string $mappedIndexerId, string
417428

418429
try {
419430
$this->updateMapping($attributeCode, $indexName);
420-
} catch (Missing404Exception $e) {
421-
unset($this->indexByCode[$mappedIndexerId . '_' . $storeId]);
422-
$indexName = $this->getIndexFromAlias($storeId, $mappedIndexerId);
423-
$this->updateMapping($attributeCode, $indexName);
431+
} catch (Exception $e) {
432+
if ($this->validateException($e)) {
433+
unset($this->indexByCode[$mappedIndexerId . '_' . $storeId]);
434+
$indexName = $this->getIndexFromAlias($storeId, $mappedIndexerId);
435+
$this->updateMapping($attributeCode, $indexName);
436+
} else {
437+
throw $e;
438+
}
424439
}
425440

426441
return $this;
427442
}
428443

444+
/**
445+
* Check if the given class name is in the exception list
446+
*
447+
* @param Exception $exception
448+
* @return bool
449+
*/
450+
private function validateException(Exception $exception): bool
451+
{
452+
return in_array(get_class($exception), $this->responseErrorExceptionList, true);
453+
}
454+
429455
/**
430456
* Retrieve index definition from class.
431457
*

app/code/Magento/Elasticsearch/Model/DataProvider/Base/Suggestions.php

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Elasticsearch\Model\DataProvider\Base;
77

88
use Elasticsearch\Common\Exceptions\BadRequest400Exception;
9+
use Exception;
910
use Magento\AdvancedSearch\Model\SuggestedQueriesInterface;
1011
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProviderInterface;
1112
use Magento\Elasticsearch\Model\Config;
@@ -70,6 +71,13 @@ class Suggestions implements SuggestedQueriesInterface
7071
*/
7172
private $getSuggestionFrequency;
7273

74+
/**
75+
* @var array
76+
*/
77+
private $responseErrorExceptionList = [
78+
'elasticsearchBadRequest404' => BadRequest400Exception::class
79+
];
80+
7381
/**
7482
* Suggestions constructor.
7583
*
@@ -82,6 +90,8 @@ class Suggestions implements SuggestedQueriesInterface
8290
* @param FieldProviderInterface $fieldProvider
8391
* @param LoggerInterface|null $logger
8492
* @param GetSuggestionFrequencyInterface|null $getSuggestionFrequency
93+
* @param array $responseErrorExceptionList
94+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
8595
*/
8696
public function __construct(
8797
ScopeConfigInterface $scopeConfig,
@@ -92,7 +102,8 @@ public function __construct(
92102
StoreManager $storeManager,
93103
FieldProviderInterface $fieldProvider,
94104
LoggerInterface $logger = null,
95-
?GetSuggestionFrequencyInterface $getSuggestionFrequency = null
105+
?GetSuggestionFrequencyInterface $getSuggestionFrequency = null,
106+
array $responseErrorExceptionList = []
96107
) {
97108
$this->queryResultFactory = $queryResultFactory;
98109
$this->connectionManager = $connectionManager;
@@ -104,6 +115,7 @@ public function __construct(
104115
$this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
105116
$this->getSuggestionFrequency = $getSuggestionFrequency ?:
106117
ObjectManager::getInstance()->get(GetSuggestionFrequencyInterface::class);
118+
$this->responseErrorExceptionList = array_merge($this->responseErrorExceptionList, $responseErrorExceptionList);
107119
}
108120

109121
/**
@@ -116,17 +128,21 @@ public function getItems(QueryInterface $query)
116128
$isResultsCountEnabled = $this->isResultsCountEnabled();
117129
try {
118130
$suggestions = $this->getSuggestions($query);
119-
} catch (BadRequest400Exception $e) {
120-
$this->logger->critical($e);
121-
$suggestions = [];
131+
} catch (Exception $e) {
132+
if ($this->validateException($e)) {
133+
$this->logger->critical($e);
134+
$suggestions = [];
135+
} else {
136+
throw $e;
137+
}
122138
}
123139

124140
foreach ($suggestions as $suggestion) {
125141
$count = null;
126142
if ($isResultsCountEnabled) {
127143
try {
128144
$count = $this->getSuggestionFrequency->execute($suggestion['text']);
129-
} catch (\Exception $e) {
145+
} catch (Exception $e) {
130146
$this->logger->critical($e);
131147
}
132148

@@ -154,6 +170,17 @@ public function isResultsCountEnabled()
154170
);
155171
}
156172

173+
/**
174+
* Check if the given class name is in the exception list
175+
*
176+
* @param Exception $exception
177+
* @return bool
178+
*/
179+
private function validateException(Exception $exception): bool
180+
{
181+
return in_array(get_class($exception), $this->responseErrorExceptionList, true);
182+
}
183+
157184
/**
158185
* Get Suggestions
159186
*
@@ -297,8 +324,7 @@ private function isSuggestionsAllowed()
297324
ScopeInterface::SCOPE_STORE
298325
);
299326
$isEnabled = $this->config->isElasticsearchEnabled();
300-
$isSuggestionsAllowed = ($isEnabled && $isSuggestionsEnabled);
301327

302-
return $isSuggestionsAllowed;
328+
return $isEnabled && $isSuggestionsEnabled;
303329
}
304330
}

app/code/Magento/Elasticsearch/SearchAdapter/ConnectionManager.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Psr\Log\LoggerInterface;
1212

1313
/**
14-
* Class provides interface for Elasticsearch connection
14+
* Class provides interface for Search Engine connection
1515
*
1616
* @api
1717
* @since 100.1.0
@@ -75,7 +75,7 @@ public function getConnection($options = [])
7575
}
7676

7777
/**
78-
* Connect to Elasticsearch client with default options
78+
* Connect to Search client with default options
7979
*
8080
* @param array $options
8181
* @throws \RuntimeException
@@ -87,7 +87,7 @@ private function connect($options)
8787
$this->client = $this->clientFactory->create($this->clientConfig->prepareClientOptions($options));
8888
} catch (\Exception $e) {
8989
$this->logger->critical($e);
90-
throw new \RuntimeException('Elasticsearch client is not set.');
90+
throw new \RuntimeException('Search client is not set.');
9191
}
9292
}
9393
}

app/code/Magento/Elasticsearch/Test/Mftf/Data/ConfigData.xml

Lines changed: 0 additions & 16 deletions
This file was deleted.

app/code/Magento/Elasticsearch/Test/Mftf/Suite/SearchEngineElasticsearchSuite.xml

Lines changed: 0 additions & 28 deletions
This file was deleted.

app/code/Magento/Elasticsearch/Test/Unit/Setup/InstallConfigTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
namespace Magento\Elasticsearch\Test\Unit\Setup;
99

10-
use Magento\Elasticsearch\Setup\InstallConfig;
1110
use Magento\Framework\App\Config\Storage\WriterInterface;
1211
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
12+
use Magento\Search\Setup\InstallConfig;
1313
use PHPUnit\Framework\MockObject\MockObject;
1414
use PHPUnit\Framework\TestCase;
1515

0 commit comments

Comments
 (0)