Skip to content

Commit 6c03839

Browse files
ENGCOM-5834: Fix elastic alias doubled index if exception was thrown #24552
- Merge Pull Request #24552 from VaD1ke/magento2:fix__elastic-alias-doubled-index_develop - Merged commits: 1. 320aac0 2. fcc38ef
2 parents d1fbf76 + fcc38ef commit 6c03839

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ public function addDocs(array $documents, $storeId, $mappedIndexerId)
193193
*/
194194
public function cleanIndex($storeId, $mappedIndexerId)
195195
{
196+
// needed to fix bug with double indices in alias because of second reindex in same process
197+
unset($this->preparedIndex[$storeId]);
198+
196199
$this->checkIndex($storeId, $mappedIndexerId, true);
197200
$indexName = $this->indexNameResolver->getIndexName($storeId, $mappedIndexerId, $this->preparedIndex);
198201
if ($this->client->isEmptyIndex($indexName)) {
@@ -203,7 +206,7 @@ public function cleanIndex($storeId, $mappedIndexerId)
203206
// prepare new index name and increase version
204207
$indexPattern = $this->indexNameResolver->getIndexPattern($storeId, $mappedIndexerId);
205208
$version = (int)(str_replace($indexPattern, '', $indexName));
206-
$newIndexName = $indexPattern . ++$version;
209+
$newIndexName = $indexPattern . (++$version);
207210

208211
// remove index if already exists
209212
if ($this->client->indexExists($newIndexName)) {
@@ -354,12 +357,14 @@ protected function prepareIndex($storeId, $indexName, $mappedIndexerId)
354357
{
355358
$this->indexBuilder->setStoreId($storeId);
356359
$settings = $this->indexBuilder->build();
357-
$allAttributeTypes = $this->fieldMapper->getAllAttributesTypes([
358-
'entityType' => $mappedIndexerId,
359-
// Use store id instead of website id from context for save existing fields mapping.
360-
// In future websiteId will be eliminated due to index stored per store
361-
'websiteId' => $storeId
362-
]);
360+
$allAttributeTypes = $this->fieldMapper->getAllAttributesTypes(
361+
[
362+
'entityType' => $mappedIndexerId,
363+
// Use store id instead of website id from context for save existing fields mapping.
364+
// In future websiteId will be eliminated due to index stored per store
365+
'websiteId' => $storeId
366+
]
367+
);
363368
$settings['index']['mapping']['total_fields']['limit'] = $this->getMappingTotalFieldsLimit($allAttributeTypes);
364369
$this->client->createIndex($indexName, ['settings' => $settings]);
365370
$this->client->addFieldsMapping(

0 commit comments

Comments
 (0)