/** * Regenerate search index for specific store * * @param int $storeId Store View Id * @param int|array $productIds Product Entity Id * @return \Generator * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function rebuildStoreIndex($storeId, $productIds = null) { if ($productIds !== null) { $productIds = array_unique(array_merge($productIds, $this->getProductIdsFromParents($productIds))); } // prepare searchable attributes $staticFields = []; foreach ($this->getSearchableAttributes('static') as $attribute) { $staticFields[] = $attribute->getAttributeCode(); } $dynamicFields = ['int' => array_keys($this->getSearchableAttributes('int')), 'varchar' => array_keys($this->getSearchableAttributes('varchar')), 'text' => array_keys($this->getSearchableAttributes('text')), 'decimal' => array_keys($this->getSearchableAttributes('decimal')), 'datetime' => array_keys($this->getSearchableAttributes('datetime'))]; // status and visibility filter $visibility = $this->getSearchableAttribute('visibility'); $status = $this->getSearchableAttribute('status'); $statusIds = $this->catalogProductStatus->getVisibleStatusIds(); $allowedVisibility = $this->engine->getAllowedVisibility(); return $this->iteratorFactory->create(['storeId' => $storeId, 'productIds' => $productIds, 'staticFields' => $staticFields, 'dynamicFields' => $dynamicFields, 'visibility' => $visibility, 'allowedVisibility' => $allowedVisibility, 'status' => $status, 'statusIds' => $statusIds]); }
/** * Regenerate search index for specific store * * @param int $storeId Store View Id * @param int|array $productIds Product Entity Id * @return \Generator * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function rebuildStoreIndex($storeId, $productIds = null) { if ($productIds !== null) { $productIds = array_unique(array_merge($productIds, $this->getProductIdsFromParents($productIds))); } // prepare searchable attributes $staticFields = []; foreach ($this->getSearchableAttributes('static') as $attribute) { $staticFields[] = $attribute->getAttributeCode(); } $dynamicFields = ['int' => array_keys($this->getSearchableAttributes('int')), 'varchar' => array_keys($this->getSearchableAttributes('varchar')), 'text' => array_keys($this->getSearchableAttributes('text')), 'decimal' => array_keys($this->getSearchableAttributes('decimal')), 'datetime' => array_keys($this->getSearchableAttributes('datetime'))]; // status and visibility filter $visibility = $this->getSearchableAttribute('visibility'); $status = $this->getSearchableAttribute('status'); $statusIds = $this->catalogProductStatus->getVisibleStatusIds(); $allowedVisibility = $this->engine->getAllowedVisibility(); $lastProductId = 0; while (true) { $products = $this->getSearchableProducts($storeId, $staticFields, $productIds, $lastProductId); if (!$products) { break; } $productAttributes = []; $productRelations = []; foreach ($products as $productData) { $lastProductId = $productData['entity_id']; $productAttributes[$productData['entity_id']] = $productData['entity_id']; $productChildren = $this->getProductChildIds($productData['entity_id'], $productData['type_id']); $productRelations[$productData['entity_id']] = $productChildren; if ($productChildren) { foreach ($productChildren as $productChildId) { $productAttributes[$productChildId] = $productChildId; } } } $productAttributes = $this->getProductAttributes($storeId, $productAttributes, $dynamicFields); foreach ($products as $productData) { if (!isset($productAttributes[$productData['entity_id']])) { continue; } $productAttr = $productAttributes[$productData['entity_id']]; if (!isset($productAttr[$visibility->getId()]) || !in_array($productAttr[$visibility->getId()], $allowedVisibility)) { continue; } if (!isset($productAttr[$status->getId()]) || !in_array($productAttr[$status->getId()], $statusIds)) { continue; } $productIndex = [$productData['entity_id'] => $productAttr]; $hasChildren = false; $productChildren = $productRelations[$productData['entity_id']]; if ($productChildren) { foreach ($productChildren as $productChildId) { if (isset($productAttributes[$productChildId])) { $productChildAttr = $productAttributes[$productChildId]; if (!isset($productChildAttr[$status->getId()]) || !in_array($productChildAttr[$status->getId()], $statusIds)) { continue; } $hasChildren = true; $productIndex[$productChildId] = $productChildAttr; } } } if ($productChildren !== null && !$hasChildren) { continue; } $index = $this->prepareProductIndex($productIndex, $productData, $storeId); (yield $productData['entity_id'] => $index); } } }