Skip to content

Commit dbe27ef

Browse files
committed
MC-38168: Manual Indexer after Merchandising - Empty Catalog/ Number of products incorrect
1 parent 59db33d commit dbe27ef

File tree

2 files changed

+101
-57
lines changed
  • app/code/Magento/Catalog/Model/Indexer

2 files changed

+101
-57
lines changed

app/code/Magento/Catalog/Model/Indexer/Category/Product/Action/Rows.php

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Magento\Catalog\Model\Config;
1818
use Magento\Catalog\Model\Category;
1919
use Magento\Catalog\Model\Indexer\Product\Category as ProductCategoryIndexer;
20+
use Magento\Catalog\Model\Indexer\Category\Product as CategoryProductIndexer;
2021
use Magento\Catalog\Model\Indexer\Category\Product\TableMaintainer;
2122
use Magento\Indexer\Model\WorkingStateProvider;
2223

@@ -110,44 +111,64 @@ public function execute(array $entityIds = [], $useTempTable = false)
110111
$this->limitationByCategories = array_unique($this->limitationByCategories);
111112
$this->useTempTable = $useTempTable;
112113
$indexer = $this->indexerRegistry->get(ProductCategoryIndexer::INDEXER_ID);
113-
$workingState = $this->workingStateProvider->isWorking($indexer->getId());
114+
$workingState = $this->getWorkingState();
114115

115-
if ($useTempTable && !$workingState && $indexer->isScheduled()) {
116-
foreach ($this->storeManager->getStores() as $store) {
117-
$this->connection->truncateTable($this->getIndexTable($store->getId()));
116+
if (!$indexer->isScheduled()
117+
|| ($indexer->isScheduled() && !$useTempTable)
118+
|| ($indexer->isScheduled() && $useTempTable && !$workingState)) {
119+
if ($useTempTable && !$workingState && $indexer->isScheduled()) {
120+
foreach ($this->storeManager->getStores() as $store) {
121+
$this->connection->truncateTable($this->getIndexTable($store->getId()));
122+
}
123+
} else {
124+
$this->removeEntries();
118125
}
119-
} else {
120-
$this->removeEntries();
121-
}
122126

123-
$this->reindex();
124-
125-
if ($useTempTable && !$workingState && $indexer->isScheduled()) {
126-
foreach ($this->storeManager->getStores() as $store) {
127-
$removalCategoryIds = array_diff($this->limitationByCategories, [$this->getRootCategoryId($store)]);
128-
$this->connection->delete(
129-
$this->tableMaintainer->getMainTable($store->getId()),
130-
['category_id IN (?)' => $removalCategoryIds]
131-
);
132-
$select = $this->connection->select()
133-
->from($this->tableMaintainer->getMainReplicaTable($store->getId()));
134-
$this->connection->query(
135-
$this->connection->insertFromSelect(
136-
$select,
127+
$this->reindex();
128+
129+
// get actual state
130+
$workingState = $this->getWorkingState();
131+
132+
if ($useTempTable && !$workingState && $indexer->isScheduled()) {
133+
foreach ($this->storeManager->getStores() as $store) {
134+
$removalCategoryIds = array_diff($this->limitationByCategories, [$this->getRootCategoryId($store)]);
135+
$this->connection->delete(
137136
$this->tableMaintainer->getMainTable($store->getId()),
138-
[],
139-
AdapterInterface::INSERT_ON_DUPLICATE
140-
)
141-
);
137+
['category_id IN (?)' => $removalCategoryIds]
138+
);
139+
$select = $this->connection->select()
140+
->from($this->tableMaintainer->getMainReplicaTable($store->getId()));
141+
$this->connection->query(
142+
$this->connection->insertFromSelect(
143+
$select,
144+
$this->tableMaintainer->getMainTable($store->getId()),
145+
[],
146+
AdapterInterface::INSERT_ON_DUPLICATE
147+
)
148+
);
149+
}
142150
}
143-
}
144151

145-
$this->registerCategories($entityIds);
146-
$this->eventManager->dispatch('clean_cache_by_tags', ['object' => $this->cacheContext]);
152+
$this->registerCategories($entityIds);
153+
$this->eventManager->dispatch('clean_cache_by_tags', ['object' => $this->cacheContext]);
154+
}
147155

148156
return $this;
149157
}
150158

159+
/**
160+
* Get state for current and shared indexer
161+
*
162+
* @return bool
163+
*/
164+
private function getWorkingState() : bool
165+
{
166+
$indexer = $this->indexerRegistry->get(ProductCategoryIndexer::INDEXER_ID);
167+
$sharedIndexer = $this->indexerRegistry->get(CategoryProductIndexer::INDEXER_ID);
168+
return $this->workingStateProvider->isWorking($indexer->getId())
169+
|| $this->workingStateProvider->isWorking($sharedIndexer->getId());
170+
}
171+
151172
/**
152173
* Register categories assigned to products
153174
*

app/code/Magento/Catalog/Model/Indexer/Product/Category/Action/Rows.php

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Magento\Store\Model\StoreManagerInterface;
1818
use Magento\Framework\DB\Adapter\AdapterInterface;
1919
use Magento\Framework\Indexer\IndexerRegistry;
20+
use Magento\Catalog\Model\Indexer\Product\Category as ProductCategoryIndexer;
2021
use Magento\Catalog\Model\Indexer\Category\Product as CategoryProductIndexer;
2122
use Magento\Catalog\Model\Indexer\Category\Product\TableMaintainer;
2223
use Magento\Indexer\Model\WorkingStateProvider;
@@ -104,46 +105,68 @@ public function execute(array $entityIds = [], $useTempTable = false)
104105
$this->limitationByProducts = $idsToBeReIndexed;
105106
$this->useTempTable = $useTempTable;
106107
$indexer = $this->indexerRegistry->get(CategoryProductIndexer::INDEXER_ID);
107-
$workingState = $this->workingStateProvider->isWorking($indexer->getId());
108+
$workingState = $this->getWorkingState();
108109

109-
$affectedCategories = $this->getCategoryIdsFromIndex($idsToBeReIndexed);
110+
if (!$indexer->isScheduled()
111+
|| ($indexer->isScheduled() && !$useTempTable)
112+
|| ($indexer->isScheduled() && $useTempTable && !$workingState)) {
110113

111-
if ($useTempTable && !$workingState && $indexer->isScheduled()) {
112-
foreach ($this->storeManager->getStores() as $store) {
113-
$this->connection->truncateTable($this->getIndexTable($store->getId()));
114+
$affectedCategories = $this->getCategoryIdsFromIndex($idsToBeReIndexed);
115+
116+
if ($useTempTable && !$workingState && $indexer->isScheduled()) {
117+
foreach ($this->storeManager->getStores() as $store) {
118+
$this->connection->truncateTable($this->getIndexTable($store->getId()));
119+
}
120+
} else {
121+
$this->removeEntries();
114122
}
115-
} else {
116-
$this->removeEntries();
117-
}
118-
$this->reindex();
119-
if ($useTempTable && !$workingState && $indexer->isScheduled()) {
120-
foreach ($this->storeManager->getStores() as $store) {
121-
$this->connection->delete(
122-
$this->tableMaintainer->getMainTable($store->getId()),
123-
['product_id IN (?)' => $this->limitationByProducts]
124-
);
125-
$select = $this->connection->select()
126-
->from($this->tableMaintainer->getMainReplicaTable($store->getId()));
127-
$this->connection->query(
128-
$this->connection->insertFromSelect(
129-
$select,
123+
$this->reindex();
124+
125+
// get actual state
126+
$workingState = $this->getWorkingState();
127+
128+
if ($useTempTable && !$workingState && $indexer->isScheduled()) {
129+
foreach ($this->storeManager->getStores() as $store) {
130+
$this->connection->delete(
130131
$this->tableMaintainer->getMainTable($store->getId()),
131-
[],
132-
AdapterInterface::INSERT_ON_DUPLICATE
133-
)
134-
);
132+
['product_id IN (?)' => $this->limitationByProducts]
133+
);
134+
$select = $this->connection->select()
135+
->from($this->tableMaintainer->getMainReplicaTable($store->getId()));
136+
$this->connection->query(
137+
$this->connection->insertFromSelect(
138+
$select,
139+
$this->tableMaintainer->getMainTable($store->getId()),
140+
[],
141+
AdapterInterface::INSERT_ON_DUPLICATE
142+
)
143+
);
144+
}
135145
}
136-
}
137146

138-
$affectedCategories = array_merge($affectedCategories, $this->getCategoryIdsFromIndex($idsToBeReIndexed));
147+
$affectedCategories = array_merge($affectedCategories, $this->getCategoryIdsFromIndex($idsToBeReIndexed));
139148

140-
$this->registerProducts($idsToBeReIndexed);
141-
$this->registerCategories($affectedCategories);
142-
$this->eventManager->dispatch('clean_cache_by_tags', ['object' => $this->cacheContext]);
149+
$this->registerProducts($idsToBeReIndexed);
150+
$this->registerCategories($affectedCategories);
151+
$this->eventManager->dispatch('clean_cache_by_tags', ['object' => $this->cacheContext]);
152+
}
143153

144154
return $this;
145155
}
146156

157+
/**
158+
* Get state for current and shared indexer
159+
*
160+
* @return bool
161+
*/
162+
private function getWorkingState() : bool
163+
{
164+
$indexer = $this->indexerRegistry->get(CategoryProductIndexer::INDEXER_ID);
165+
$sharedIndexer = $this->indexerRegistry->get(ProductCategoryIndexer::INDEXER_ID);
166+
return $this->workingStateProvider->isWorking($indexer->getId())
167+
|| $this->workingStateProvider->isWorking($sharedIndexer->getId());
168+
}
169+
147170
/**
148171
* Get IDs of parent products by their child IDs.
149172
*

0 commit comments

Comments
 (0)