diff --git a/src/Solr/Implementor/ProductRepository.php b/src/Solr/Implementor/ProductRepository.php index a5ac9ff..51279c0 100755 --- a/src/Solr/Implementor/ProductRepository.php +++ b/src/Solr/Implementor/ProductRepository.php @@ -23,9 +23,11 @@ interface ProductRepository public function getProductsInChunks($storeId, ProductIdChunks $chunks); /** + * @param null|int $sliceId + * @param null|int $totalNumberSlices * @return int[] */ - public function getAllProductIds(); + public function getAllProductIds($sliceId = null, $totalNumberSlices = null); /** * @param null|int[] $productIds diff --git a/src/Solr/Indexer/ProductIndexer.php b/src/Solr/Indexer/ProductIndexer.php index 21c5daa..5b8e1c0 100755 --- a/src/Solr/Indexer/ProductIndexer.php +++ b/src/Solr/Indexer/ProductIndexer.php @@ -93,13 +93,15 @@ protected function _getStoreConfig($storeId = null) * @param array|null $productIds Restrict to given Products if this is set * @param boolean|string $emptyIndex Whether to truncate the index before refilling it * @param null|int[] $restrictToStoreIds + * @param null|int $sliceId + * @param null|int $totalNumberSlices * @throws \Exception * @throws \IntegerNet\Solr\Exception */ - public function reindex($productIds = null, $emptyIndex = false, $restrictToStoreIds = null) + public function reindex($productIds = null, $emptyIndex = false, $restrictToStoreIds = null, $sliceId = null, $totalNumberSlices = null) { - if (is_null($productIds)) { - $this->_getResource()->checkSwapCoresConfiguration($restrictToStoreIds); + if (is_null($productIds) && is_null($sliceId)) { + $this->checkSwapCoresConfiguration($restrictToStoreIds); } foreach($this->_config as $storeId => $storeConfig) { @@ -114,15 +116,15 @@ public function reindex($productIds = null, $emptyIndex = false, $restrictToStor $this->storeEmulation->start($storeId); try { - if (is_null($productIds) && $storeConfig->getIndexingConfig()->isSwapCores()) { - $this->_getResource()->setUseSwapIndex(); + if (is_null($productIds) && is_null($sliceId) && $storeConfig->getIndexingConfig()->isSwapCores()) { + $this->activateSwapCore(); } if ( - ($emptyIndex && $storeConfig->getIndexingConfig()->isDeleteDocumentsBeforeIndexing()) + ($emptyIndex && is_null($sliceId) && $storeConfig->getIndexingConfig()->isDeleteDocumentsBeforeIndexing()) || $emptyIndex === 'force' ) { - $this->_getResource()->deleteAllDocuments($storeId, self::CONTENT_TYPE); + $this->clearIndex($storeId); } $pageSize = intval($storeConfig->getIndexingConfig()->getPagesize()); @@ -130,15 +132,23 @@ public function reindex($productIds = null, $emptyIndex = false, $restrictToStor $pageSize = 100; } - $associations = $this->productRepository->getProductAssociations($productIds); + if ($productIds == null) { + $productIdsToIndex = $this->productRepository->getAllProductIds($sliceId, $totalNumberSlices); + } else { + $productIdsToIndex = $productIds; + } + + $associations = $this->productRepository->getProductAssociations($productIdsToIndex); $chunks = ProductIdChunks::withAssociationsTogether( - $productIds == null ? $this->productRepository->getAllProductIds() : $productIds, + $productIdsToIndex, $associations, $pageSize); $productIterator = $this->productRepository->getProductsInChunks($storeId, $chunks); - $this->_indexProductCollection($emptyIndex, $productIterator, $storeId, $productIds, $associations); + $this->_indexProductCollection($emptyIndex, $productIterator, $storeId, $productIdsToIndex, $associations); - $this->_getResource()->setUseSwapIndex(false); + if (is_null($productIds) && is_null($sliceId) && $storeConfig->getIndexingConfig()->isSwapCores()) { + $this->deactivateSwapCore(); + } } catch (\Exception $e) { $this->storeEmulation->stop(); throw $e; @@ -146,8 +156,8 @@ public function reindex($productIds = null, $emptyIndex = false, $restrictToStor $this->storeEmulation->stop(); } - if (is_null($productIds)) { - $this->_getResource()->swapCores($restrictToStoreIds); + if (is_null($productIds) && is_null($sliceId)) { + $this->swapCores($restrictToStoreIds); } } @@ -514,5 +524,39 @@ protected function _indexProductCollection($emptyIndex, PagedProductIterator $pr return $storeId; } + /** + * @param $storeId + */ + public function clearIndex($storeId) + { + $this->_getResource()->deleteAllDocuments($storeId, self::CONTENT_TYPE); + } + + public function activateSwapCore() + { + $this->_getResource()->setUseSwapIndex(); + } + + public function deactivateSwapCore() + { + $this->_getResource()->setUseSwapIndex(false); + } + + /** + * @param null|int[] $restrictToStoreIds + */ + public function swapCores($restrictToStoreIds) + { + $this->_getResource()->swapCores($restrictToStoreIds); + } + + /** + * @param $restrictToStoreIds + */ + public function checkSwapCoresConfiguration($restrictToStoreIds) + { + return $this->_getResource()->checkSwapCoresConfiguration($restrictToStoreIds); + } + } \ No newline at end of file