Skip to content

Commit 9bd63d9

Browse files
committed
MAGE-1122 Add missing price handler service for delta index
1 parent c5c771b commit 9bd63d9

File tree

2 files changed

+82
-31
lines changed

2 files changed

+82
-31
lines changed

Helper/Data.php

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper;
1313
use Algolia\AlgoliaSearch\Helper\Entity\SuggestionHelper;
1414
use Algolia\AlgoliaSearch\Service\IndexNameFetcher;
15+
use Algolia\AlgoliaSearch\Service\Product\MissingPriceIndexHandler;
1516
use Magento\Catalog\Model\Category;
1617
use Magento\Catalog\Model\Product;
1718
use Magento\Catalog\Model\ResourceModel\Product\Collection;
@@ -35,21 +36,22 @@ class Data
3536
protected IndexerInterface $priceIndexer;
3637

3738
public function __construct(
38-
protected AlgoliaHelper $algoliaHelper,
39-
protected ConfigHelper $configHelper,
40-
protected ProductHelper $productHelper,
41-
protected CategoryHelper $categoryHelper,
42-
protected PageHelper $pageHelper,
43-
protected SuggestionHelper $suggestionHelper,
44-
protected AdditionalSectionHelper $additionalSectionHelper,
45-
protected Emulation $emulation,
46-
protected Logger $logger,
47-
protected ResourceConnection $resource,
48-
protected ManagerInterface $eventManager,
49-
protected ScopeCodeResolver $scopeCodeResolver,
50-
protected StoreManagerInterface $storeManager,
51-
protected IndexNameFetcher $indexNameFetcher,
52-
IndexerRegistry $indexerRegistry
39+
protected AlgoliaHelper $algoliaHelper,
40+
protected ConfigHelper $configHelper,
41+
protected ProductHelper $productHelper,
42+
protected CategoryHelper $categoryHelper,
43+
protected PageHelper $pageHelper,
44+
protected SuggestionHelper $suggestionHelper,
45+
protected AdditionalSectionHelper $additionalSectionHelper,
46+
protected Emulation $emulation,
47+
protected Logger $logger,
48+
protected ResourceConnection $resource,
49+
protected ManagerInterface $eventManager,
50+
protected ScopeCodeResolver $scopeCodeResolver,
51+
protected StoreManagerInterface $storeManager,
52+
protected IndexNameFetcher $indexNameFetcher,
53+
protected MissingPriceIndexHandler $missingPriceIndexHandler,
54+
IndexerRegistry $indexerRegistry
5355
)
5456
{
5557
$this->priceIndexer = $indexerRegistry->get('catalog_product_price');
@@ -78,7 +80,7 @@ public function deleteObjects(int $storeId, array $ids, string $indexName): void
7880
$this->algoliaHelper->deleteObjects($ids, $indexName);
7981
}
8082

81-
/**
83+
/**`
8284
* @param string $query
8385
* @param int $storeId
8486
* @param array|null $searchParams
@@ -370,7 +372,7 @@ public function rebuildStoreProductIndex(int $storeId, array $productIds): void
370372
return;
371373
}
372374

373-
$this->checkPriceIndex($productIds);
375+
$this->missingPriceIndexHandler->refreshPriceIndex($productIds);
374376

375377
$this->startEmulation($storeId);
376378
$this->logger->start('Indexing');
@@ -936,18 +938,4 @@ protected function deleteInactiveIds($storeId, $objectIds, $indexName): void
936938
$idsToDeleteFromAlgolia = array_diff($objectIds, $dbIds);
937939
$this->algoliaHelper->deleteObjects($idsToDeleteFromAlgolia, $indexName);
938940
}
939-
940-
/**
941-
* If the price index is stale
942-
* @param array $productIds
943-
* @return void
944-
*/
945-
protected function checkPriceIndex(array $productIds): void
946-
{
947-
$state = $this->priceIndexer->getState()->getStatus();
948-
if ($state === \Magento\Framework\Indexer\StateInterface::STATUS_INVALID) {
949-
$this->priceIndexer->reindexList($productIds);
950-
}
951-
}
952-
953941
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Service\Product;
4+
5+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
6+
use Magento\Framework\Indexer\IndexerRegistry;
7+
use Magento\Framework\Indexer\IndexerInterface;
8+
9+
class MissingPriceIndexHandler
10+
{
11+
protected IndexerInterface $indexer;
12+
public function __construct(
13+
protected CollectionFactory $productCollectionFactory,
14+
IndexerRegistry $indexerRegistry
15+
)
16+
{
17+
$this->indexer = $indexerRegistry->get('catalog_product_price');
18+
}
19+
20+
/**
21+
* @param array $productIds
22+
* @return int[] Array of product IDs that were reindexed by this repair operation
23+
*/
24+
public function refreshPriceIndex(array $productIds): array
25+
{
26+
$reindexIds = $this->getProductIdsToReindex($productIds);
27+
if (empty($reindexIds)) {
28+
return [];
29+
}
30+
31+
$this->indexer->reindexList($reindexIds);
32+
33+
return $reindexIds;
34+
}
35+
36+
/**
37+
* @param int[] $productIds
38+
* @return int[]
39+
*/
40+
protected function getProductIdsToReindex(array $productIds): array
41+
{
42+
$state = $this->indexer->getState()->getStatus();
43+
if ($state === \Magento\Framework\Indexer\StateInterface::STATUS_INVALID) {
44+
return $productIds;
45+
}
46+
47+
$collection = $this->productCollectionFactory->create();
48+
49+
$collection->addAttributeToSelect(['name', 'price']);
50+
51+
$collection->getSelect()->joinLeft(
52+
['price_index' => 'catalog_product_index_price'],
53+
'e.entity_id = price_index.entity_id',
54+
[]
55+
);
56+
57+
$collection->getSelect()
58+
->where('price_index.entity_id IS NULL')
59+
->where('entity_id IN (?)', $productIds);
60+
61+
return $collection->getAllIds();
62+
}
63+
}

0 commit comments

Comments
 (0)