Skip to content

Commit ec9b78f

Browse files
author
Oleksandr Iegorov
committed
MAGETWO-97915: Catalog Search index is taking hours to complete
1 parent 8dfe26a commit ec9b78f

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@
1212
use Magento\Elasticsearch\Model\Adapter\BatchDataMapperInterface;
1313
use Magento\Elasticsearch\Model\Adapter\FieldType\Date as DateFieldType;
1414
use Magento\AdvancedSearch\Model\Adapter\DataMapper\AdditionalFieldsProviderInterface;
15+
use Magento\Eav\Api\Data\AttributeOptionInterface;
1516

1617
/**
1718
* Map product index data to search engine metadata
1819
*/
1920
class ProductDataMapper implements BatchDataMapperInterface
2021
{
22+
/**
23+
* @var AttributeOptionInterface[]
24+
*/
25+
private $attributeOptionsCache;
26+
2127
/**
2228
* @var Builder
2329
*/
@@ -95,6 +101,7 @@ public function __construct(
95101
$this->excludedAttributes = array_merge($this->defaultExcludedAttributes, $excludedAttributes);
96102
$this->additionalFieldsProvider = $additionalFieldsProvider;
97103
$this->dataProvider = $dataProvider;
104+
$this->attributeOptionsCache = [];
98105
}
99106

100107
/**
@@ -272,7 +279,13 @@ private function isAttributeDate(Attribute $attribute): bool
272279
private function getValuesLabels(Attribute $attribute, array $attributeValues): array
273280
{
274281
$attributeLabels = [];
275-
foreach ($attribute->getOptions() as $option) {
282+
283+
$options = $this->getAttributeOptions($attribute);
284+
if (empty($options)) {
285+
return $attributeLabels;
286+
}
287+
288+
foreach ($options as $option) {
276289
if (\in_array($option->getValue(), $attributeValues)) {
277290
$attributeLabels[] = $option->getLabel();
278291
}
@@ -281,6 +294,22 @@ private function getValuesLabels(Attribute $attribute, array $attributeValues):
281294
return $attributeLabels;
282295
}
283296

297+
/**
298+
* Retrieve options for attribute
299+
*
300+
* @param Attribute $attribute
301+
* @return array
302+
*/
303+
private function getAttributeOptions(Attribute $attribute): array
304+
{
305+
if (!isset($this->attributeOptionsCache[$attribute->getId()])) {
306+
$options = $attribute->getOptions() ?? [];
307+
$this->attributeOptionsCache[$attribute->getId()] = $options;
308+
}
309+
310+
return $this->attributeOptionsCache[$attribute->getId()];
311+
}
312+
284313
/**
285314
* Retrieve value for field. If field have only one value this method return it.
286315
* Otherwise will be returned array of these values.

0 commit comments

Comments
 (0)