Skip to content

Commit e182353

Browse files
committed
MC-31304: [ElasticSearch] Exception on catalog search result page
1 parent f98af39 commit e182353

File tree

1 file changed

+66
-8
lines changed

1 file changed

+66
-8
lines changed

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

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ class Elasticsearch
7272
*/
7373
private $batchDocumentDataMapper;
7474

75+
/**
76+
* @var array
77+
*/
78+
private $mappedAttributes = [];
79+
80+
/**
81+
* @var string[]
82+
*/
83+
private $indexByCode = [];
84+
7585
/**
7686
* @var ArrayManager
7787
*/
@@ -349,7 +359,7 @@ public function updateAlias($storeId, $mappedIndexerId)
349359
*/
350360
public function updateIndexMapping(int $storeId, string $mappedIndexerId): self
351361
{
352-
$indexName = $this->indexNameResolver->getIndexFromAlias($storeId, $mappedIndexerId);
362+
$indexName = $this->getIndexFromAlias($storeId, $mappedIndexerId);
353363
if (empty($indexName)) {
354364
return $this;
355365
}
@@ -359,19 +369,67 @@ public function updateIndexMapping(int $storeId, string $mappedIndexerId): self
359369
'websiteId' => $storeId,
360370
]);
361371

362-
$mappedAttributes = $this->client->getMapping(['index' => $indexName]);
363-
$pathField = $this->arrayManager->findPath('properties', $mappedAttributes);
364-
$mappedAttributes = $this->arrayManager->get($pathField, $mappedAttributes, []);
365-
366-
$settings['index']['mapping']['total_fields']['limit'] = $this->getMappingTotalFieldsLimit($allAttributeTypes);
367-
$this->client->putIndexSettings($indexName, ['settings' => $settings]);
368-
$attrToUpdate = array_diff_key($allAttributeTypes, $mappedAttributes);
372+
$attrToUpdate = array_diff_key($allAttributeTypes, $this->getMappedAttributes($indexName));
369373
if (!empty($attrToUpdate)) {
374+
$settings['index']['mapping']['total_fields']['limit'] = $this
375+
->getMappingTotalFieldsLimit($allAttributeTypes);
376+
$this->client->putIndexSettings($indexName, ['settings' => $settings]);
377+
370378
$this->client->addFieldsMapping(
371379
$attrToUpdate,
372380
$indexName,
373381
$this->clientConfig->getEntityType()
374382
);
383+
$this->setMappedAttributes($attrToUpdate);
384+
}
385+
386+
return $this;
387+
}
388+
389+
/**
390+
* Retrieve index definition from cache.
391+
*
392+
* @param int $storeId
393+
* @param string $mappedIndexerId
394+
* @return string
395+
*/
396+
private function getIndexFromAlias(int $storeId, string $mappedIndexerId): string
397+
{
398+
$indexCode = $mappedIndexerId . $storeId;
399+
if (!isset($this->indexByCode[$indexCode])) {
400+
$this->indexByCode[$indexCode] = $this->indexNameResolver->getIndexFromAlias($storeId, $mappedIndexerId);
401+
}
402+
403+
return $this->indexByCode[$indexCode];
404+
}
405+
406+
/**
407+
* Retrieve mapped attributes from cache.
408+
*
409+
* @param string $indexName
410+
* @return array
411+
*/
412+
private function getMappedAttributes(string $indexName): array
413+
{
414+
if (empty($this->mappedAttributes)) {
415+
$mappedAttributes = $this->client->getMapping(['index' => $indexName]);
416+
$pathField = $this->arrayManager->findPath('properties', $mappedAttributes);
417+
$this->mappedAttributes = $this->arrayManager->get($pathField, $mappedAttributes, []);
418+
}
419+
420+
return $this->mappedAttributes;
421+
}
422+
423+
/**
424+
* Set mapped attributes to cache.
425+
*
426+
* @param array $mappedAttributes
427+
* @return $this
428+
*/
429+
private function setMappedAttributes(array $mappedAttributes): self
430+
{
431+
foreach ($mappedAttributes as $attributeCode => $attributeParams) {
432+
$this->mappedAttributes[$attributeCode] = $attributeParams;
375433
}
376434

377435
return $this;

0 commit comments

Comments
 (0)