public function testResetSearchResult() { $this->resource->expects($this->once())->method('getTableName')->with('search_query', 'core_read')->willReturn('table_name_search_query'); $this->adapter->expects($this->once())->method('update')->with('table_name_search_query', ['is_processed' => 0], ['is_processed != 0'])->willReturn(10); $result = $this->target->resetSearchResults(); $this->assertEquals($this->target, $result); }
public function testExecuteFull() { $stores = [0 => 'Store 1', 1 => 'Store 2']; $indexData = new \ArrayObject([]); $this->storeManager->expects($this->once())->method('getStores')->willReturn($stores); $this->saveHandler->expects($this->exactly(count($stores)))->method('cleanIndex'); $this->saveHandler->expects($this->exactly(count($stores)))->method('saveIndex'); $this->fullAction->expects($this->exactly(count($stores)))->method('rebuildStoreIndex')->willReturn($indexData); $this->fulltextResource->expects($this->once())->method('resetSearchResults'); $this->searchRequestConfig->expects($this->once())->method('reset'); $this->model->executeFull(); }
/** * Search the text and return result collection * * @param string $text * @return \Magento\Catalog\Model\Product[] */ protected function search($text) { $this->resourceFulltext->resetSearchResults(); $query = $this->queryFactory->get(); $query->unsetData()->setQueryText($text)->prepare(); $products = []; $collection = Bootstrap::getObjectManager()->create(Collection::class); $collection->addSearchFilter($text); foreach ($collection as $product) { $products[] = $product; } return $products; }
/** * Search the text and return result collection * * @param string $text * @return \Magento\Catalog\Model\Product[] */ protected function search($text) { $this->resourceFulltext->resetSearchResults(); $query = $this->queryFactory->create(); $query->setQueryText($text)->prepare(); $this->resourceFulltext->prepareResult($this->fulltext, $text, $query); $query->getResultCollection(); $products = []; foreach ($query->getResultCollection() as $product) { $products[] = $product; } return $products; }
/** * Execute full indexation * * @return void */ public function executeFull() { $storeIds = array_keys($this->storeManager->getStores()); /** @var IndexerHandler $saveHandler */ $saveHandler = $this->indexerHandlerFactory->create(['data' => $this->data]); foreach ($storeIds as $storeId) { $dimension = $this->dimensionFactory->create(['name' => 'scope', 'value' => $storeId]); $saveHandler->cleanIndex([$dimension]); $saveHandler->saveIndex([$dimension], $this->fullAction->rebuildStoreIndex($storeId)); } $this->fulltextResource->resetSearchResults(); $this->searchRequestConfig->reset(); }
/** * Regenerate search index for specific store * * @param int $storeId Store View Id * @param int|array $productIds Product Entity Id * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ protected function rebuildStoreIndex($storeId, $productIds = null) { $this->cleanIndex($storeId, $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->engineProvider->get()->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; } } } $productIndexes = []; $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); $productIndexes[$productData['entity_id']] = $index; } $this->saveProductIndexes($storeId, $productIndexes); } $this->fulltextResource->resetSearchResults(); }
/** * Regenerate search index for all stores * * @param int|array|null $productIds * @return void */ protected function rebuildIndex($productIds = null) { $storeIds = array_keys($this->storeManager->getStores()); foreach ($storeIds as $storeId) { $dimension = $this->dimensionFactory->create(['name' => self::SCOPE_FIELD_NAME, 'value' => $storeId]); $this->indexHandler->deleteIndex([$dimension], $this->getIterator($productIds)); $this->indexHandler->saveIndex([$dimension], $this->rebuildStoreIndex($storeId, $productIds)); } $this->fulltextResource->resetSearchResults(); $this->searchRequestConfig->reset(); }