Skip to content

Commit 8a001f6

Browse files
committed
MAGE-941 Remove index name requirement from ReplicaManager and getSortingIndices - extrapolate from known data instead
1 parent ae84619 commit 8a001f6

File tree

6 files changed

+54
-42
lines changed

6 files changed

+54
-42
lines changed

Api/Product/ReplicaManagerInterface.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@ interface ReplicaManagerInterface
1515
public const SORT_KEY_VIRTUAL_REPLICA = 'virtualReplica';
1616
public const MAX_VIRTUAL_REPLICA_LIMIT = 20;
1717

18-
1918
/**
2019
* Configure replicas in Algolia based on the sorting configuration in Magento
2120
*
22-
* @param string $primaryIndexName Could be tmp (legacy impl)
2321
* @param int $storeId
2422
* @param array<string, mixed> $primaryIndexSettings
2523
* @return void
@@ -29,8 +27,7 @@ interface ReplicaManagerInterface
2927
* @throws LocalizedException
3028
* @throws NoSuchEntityException
3129
*/
32-
public function syncReplicasToAlgolia(string $primaryIndexName, int $storeId, array $primaryIndexSettings): void;
33-
30+
public function syncReplicasToAlgolia(int $storeId, array $primaryIndexSettings): void;
3431

3532
/**
3633
* For standard Magento front end (e.g. Luma) replicas will likely only be needed if InstantSearch is enabled

Block/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ public function getConfiguration()
256256
'areCategoriesInFacets' => $areCategoriesInFacets,
257257
'hitsPerPage' => (int) $config->getNumberOfProductResults(),
258258
'sortingIndices' => array_values($config->getSortingIndices(
259-
$coreHelper->getIndexName($productHelper->getIndexNameSuffix()),
259+
$this->getStoreId(),
260260
null,
261261
$customerGroupId
262262
)),

Helper/ConfigHelper.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Algolia\AlgoliaSearch\Helper;
44

55
use Algolia\AlgoliaSearch\Api\Product\ReplicaManagerInterface;
6+
use Algolia\AlgoliaSearch\Service\IndexNameFetcher;
67
use Magento;
78
use Magento\Cookie\Helper\Cookie as CookieHelper;
89
use Magento\Customer\Api\GroupExcludedWebsiteRepositoryInterface;
@@ -11,6 +12,7 @@
1112
use Magento\Framework\App\Filesystem\DirectoryList;
1213
use Magento\Framework\DataObject;
1314
use Magento\Framework\Exception\LocalizedException;
15+
use Magento\Framework\Exception\NoSuchEntityException;
1416
use Magento\Framework\Locale\Currency;
1517
use Magento\Framework\Serialize\SerializerInterface;
1618
use Magento\Store\Model\ScopeInterface;
@@ -186,7 +188,8 @@ public function __construct(
186188
protected SerializerInterface $serializer,
187189
protected GroupCollection $groupCollection,
188190
protected GroupExcludedWebsiteRepositoryInterface $groupExcludedWebsiteRepository,
189-
protected CookieHelper $cookieHelper
191+
protected CookieHelper $cookieHelper,
192+
protected IndexNameFetcher $indexNameFetcher
190193
)
191194
{}
192195

@@ -1103,21 +1106,18 @@ protected function isGroupPricingExcludedFromWebsite(int $customerGroupId, int $
11031106
/**
11041107
* Augment sorting configuration with corresponding replica indices, ranking,
11051108
* and (as needed) customer group pricing
1106-
* TODO: MAGE-941 Remove the $originalIndexName param - this should never be needed as tmp indices cannot have attached replicas
11071109
*
1108-
* @param string $originalIndexName
11091110
* @param ?int $storeId
11101111
* @param ?int $currentCustomerGroupId
11111112
* @param ?array $attrs - serialized array of sorting attributes to transform (defaults to saved sorting config)
11121113
* @return array of transformed sorting / replica objects
1113-
* @throws Magento\Framework\Exception\LocalizedException
1114-
* @throws Magento\Framework\Exception\NoSuchEntityException
1114+
* @throws LocalizedException
1115+
* @throws NoSuchEntityException
11151116
*/
11161117
public function getSortingIndices(
1117-
string $originalIndexName,
1118-
int $storeId = null,
1119-
int $currentCustomerGroupId = null,
1120-
array $attrs = null
1118+
?int $storeId = null,
1119+
?int $currentCustomerGroupId = null,
1120+
?array $attrs = null
11211121
): array
11221122
{
11231123
// Selectively cache this result - only cache manipulation of saved settings per store
@@ -1134,6 +1134,7 @@ public function getSortingIndices(
11341134
$attrs = $this->getSorting($storeId);
11351135
}
11361136

1137+
$primaryIndexName = $this->indexNameFetcher->getProductIndexName($storeId);
11371138
$currency = $this->getCurrencyCode($storeId);
11381139
$attributesToAdd = [];
11391140
foreach ($attrs as $key => $attr) {
@@ -1149,17 +1150,17 @@ public function getSortingIndices(
11491150
foreach ($groupCollection as $group) {
11501151
$customerGroupId = (int) $group->getData('customer_group_id');
11511152
if (!$this->isGroupPricingExcludedFromWebsite($customerGroupId, $websiteId)) {
1152-
$newAttr = $this->getCustomerGroupSortPriceOverride($originalIndexName, $customerGroupId, $currency, $attr);;
1153+
$newAttr = $this->getCustomerGroupSortPriceOverride($primaryIndexName, $customerGroupId, $currency, $attr);;
11531154
$attributesToAdd[$newAttr['sort']][] = $this->decorateSortAttribute($newAttr);
11541155
}
11551156
}
11561157
// Regular pricing
11571158
} elseif ($attr[ReplicaManagerInterface::SORT_KEY_ATTRIBUTE_NAME] === ReplicaManagerInterface::SORT_ATTRIBUTE_PRICE) {
1158-
$indexName = $originalIndexName . '_' . $attr['attribute'] . '_' . 'default' . '_' . $attr['sort'];
1159+
$indexName = $primaryIndexName . '_' . $attr['attribute'] . '_' . 'default' . '_' . $attr['sort'];
11591160
$sortAttribute = $attr['attribute'] . '.' . $currency . '.' . 'default';
11601161
// All other sort attributes
11611162
} else {
1162-
$indexName = $originalIndexName . '_' . $attr['attribute'] . '_' . $attr['sort'];
1163+
$indexName = $primaryIndexName . '_' . $attr['attribute'] . '_' . $attr['sort'];
11631164
$sortAttribute = $attr['attribute'];
11641165
}
11651166

@@ -1190,10 +1191,10 @@ public function getSortingIndices(
11901191
}
11911192

11921193
/***
1193-
* @param $storeId
1194+
* @param int|null $storeId
11941195
* @return array<string,<array<string, mixed>>>
11951196
*/
1196-
public function getSorting($storeId = null): array
1197+
public function getSorting(?int $storeId = null): array
11971198
{
11981199
return $this->unserialize($this->getRawSortingValue($storeId));
11991200
}

Helper/Entity/ProductHelper.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
class ProductHelper extends AbstractEntityHelper
4343
{
44+
public const INDEX_NAME_SUFFIX = '_products';
4445
/**
4546
* @var AbstractType[]
4647
*/
@@ -117,7 +118,7 @@ public function __construct(
117118
*/
118119
public function getIndexNameSuffix(): string
119120
{
120-
return '_products';
121+
return self::INDEX_NAME_SUFFIX;
121122
}
122123

123124
/**
@@ -355,7 +356,7 @@ public function setSettings(string $indexName, string $indexNameTmp, int $storeI
355356
$this->setFacetsQueryRules($indexNameTmp);
356357
}
357358

358-
$this->replicaManager->syncReplicasToAlgolia($indexName, $storeId, $indexSettings);
359+
$this->replicaManager->syncReplicasToAlgolia($storeId, $indexSettings);
359360

360361
if ($saveToTmpIndicesToo) {
361362
try {
@@ -1415,7 +1416,7 @@ function($sort) {
14151416
*/
14161417
public function handlingReplica(string $indexName, int $storeId, array|bool $sortingAttribute = false): void
14171418
{
1418-
$sortingIndices = $this->configHelper->getSortingIndices($indexName, $storeId, null, $sortingAttribute);
1419+
$sortingIndices = $this->configHelper->getSortingIndices($storeId, null, $sortingAttribute);
14191420
if ($this->configHelper->isInstantEnabled($storeId)) {
14201421
$newReplicas = $this->decorateReplicasSetting($sortingIndices);
14211422

Service/IndexNameFetcher.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Algolia\AlgoliaSearch\Service;
44

55
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
6+
use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper;
67
use Magento\Framework\Exception\NoSuchEntityException;
78
use Magento\Store\Model\StoreManagerInterface;
89

@@ -39,4 +40,12 @@ public function getBaseIndexName(?int $storeId = null): string
3940
return $this->configHelper->getIndexPrefix($storeId) . $this->storeManager->getStore($storeId)->getCode();
4041
}
4142

43+
/**
44+
* @throws NoSuchEntityException
45+
*/
46+
public function getProductIndexName(int $storeId, bool $tmp = false): string
47+
{
48+
return $this->getIndexName(ProductHelper::INDEX_NAME_SUFFIX, $storeId, $tmp);
49+
}
50+
4251
}

Service/Product/ReplicaManager.php

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
use Algolia\AlgoliaSearch\Exception\ReplicaLimitExceededException;
99
use Algolia\AlgoliaSearch\Exception\TooManyCustomerGroupsAsReplicasException;
1010
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
11+
use Algolia\AlgoliaSearch\Exceptions\ExceededRetriesException;
1112
use Algolia\AlgoliaSearch\Helper\AlgoliaHelper;
1213
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
1314
use Algolia\AlgoliaSearch\Helper\Logger;
1415
use Algolia\AlgoliaSearch\Registry\ReplicaState;
16+
use Algolia\AlgoliaSearch\Service\IndexNameFetcher;
1517
use Algolia\AlgoliaSearch\Validator\VirtualReplicaValidatorFactory;
1618
use Magento\Framework\Exception\LocalizedException;
1719
use Magento\Framework\Exception\NoSuchEntityException;
@@ -50,6 +52,7 @@ public function __construct(
5052
protected AlgoliaHelper $algoliaHelper,
5153
protected ReplicaState $replicaState,
5254
protected VirtualReplicaValidatorFactory $validatorFactory,
55+
protected IndexNameFetcher $indexNameFetcher,
5356
protected Logger $logger
5457
)
5558
{}
@@ -62,7 +65,7 @@ public function __construct(
6265
* @throws NoSuchEntityException
6366
* @throws LocalizedException
6467
*/
65-
protected function hasReplicaConfigurationChanged(string $primaryIndexName, int $storeId): bool
68+
protected function hasReplicaConfigurationChanged(int $storeId): bool
6669
{
6770
switch ($this->replicaState->getChangeState($storeId)) {
6871
case ReplicaState::REPLICA_STATE_CHANGED:
@@ -71,14 +74,16 @@ protected function hasReplicaConfigurationChanged(string $primaryIndexName, int
7174
return false;
7275
case ReplicaState::REPLICA_STATE_UNKNOWN:
7376
default:
77+
$primaryIndexName = $this->indexNameFetcher->getProductIndexName($storeId);
7478
$old = $this->getMagentoReplicaConfigurationFromAlgolia($primaryIndexName);
75-
$new = $this->transformSortingIndicesToReplicaSetting($this->configHelper->getSortingIndices($primaryIndexName, $storeId));
79+
$new = $this->transformSortingIndicesToReplicaSetting($this->configHelper->getSortingIndices($storeId));
7680
sort($old);
7781
sort($new);
7882
return $old !== $new;
7983
}
8084
}
8185

86+
8287
/**
8388
* @param $primaryIndexName
8489
* @param bool $refreshCache
@@ -183,20 +188,19 @@ function ($sort) use ($mode) {
183188
* In order to avoid interfering with replicas configured directly in the Algolia dashboard,
184189
* we must know which replica indices are Magento managed and which are not.
185190
*
186-
* @param string $primaryIndexName
187191
* @param int $storeId
188192
* @param bool $refreshCache
189193
* @return array
190194
* @throws LocalizedException
191195
* @throws NoSuchEntityException
192196
*/
193-
protected function getMagentoReplicaSettingsFromConfig(string $primaryIndexName, int $storeId, bool $refreshCache = false): array
197+
protected function getMagentoReplicaSettingsFromConfig(int $storeId, bool $refreshCache = false): array
194198
{
195199
if ($refreshCache || !isset($this->_magentoReplicaPossibleConfig[$storeId])) {
196200
$sortConfig = $this->replicaState->getChangeState($storeId) === ReplicaState::REPLICA_STATE_CHANGED
197201
? array_merge($this->replicaState->getOriginalSortConfiguration($storeId), $this->replicaState->getUpdatedSortConfiguration($storeId))
198202
: null;
199-
$sortingIndices = $this->configHelper->getSortingIndices($primaryIndexName, $storeId, null, $sortConfig);
203+
$sortingIndices = $this->configHelper->getSortingIndices($storeId, null, $sortConfig);
200204
$this->_magentoReplicaPossibleConfig[$storeId] = array_merge(
201205
$this->transformSortingIndicesToReplicaSetting($sortingIndices, self::REPLICA_TRANSFORM_MODE_STANDARD),
202206
$this->transformSortingIndicesToReplicaSetting($sortingIndices, self::REPLICA_TRANSFORM_MODE_VIRTUAL)
@@ -208,27 +212,28 @@ protected function getMagentoReplicaSettingsFromConfig(string $primaryIndexName,
208212
/**
209213
* @inheritDoc
210214
*/
211-
public function syncReplicasToAlgolia(string $primaryIndexName, int $storeId, array $primaryIndexSettings): void
215+
public function syncReplicasToAlgolia(int $storeId, array $primaryIndexSettings): void
212216
{
213217
if ($this->isReplicaSyncEnabled($storeId)
214-
&& $this->hasReplicaConfigurationChanged($primaryIndexName, $storeId)
215-
&& $this->isReplicaConfigurationValid($primaryIndexName, $storeId)) {
216-
$addedReplicas = $this->setReplicasOnPrimaryIndex($primaryIndexName, $storeId);
217-
$this->configureRanking($primaryIndexName, $storeId, $addedReplicas, $primaryIndexSettings);
218+
&& $this->hasReplicaConfigurationChanged($storeId)
219+
&& $this->isReplicaConfigurationValid($storeId)) {
220+
$addedReplicas = $this->setReplicasOnPrimaryIndex($storeId);
221+
$this->configureRanking($storeId, $addedReplicas, $primaryIndexSettings);
218222
}
219223
}
220224

221225
/**
222-
* @param string $indexName
223226
* @param int $storeId
224227
* @return string[] Replicas added or modified by this operation
228+
* @throws AlgoliaException
225229
* @throws LocalizedException
226230
* @throws NoSuchEntityException
227-
* @throws AlgoliaException
231+
* @throws ExceededRetriesException
228232
*/
229-
protected function setReplicasOnPrimaryIndex(string $indexName, int $storeId): array
233+
protected function setReplicasOnPrimaryIndex(int $storeId): array
230234
{
231-
$sortingIndices = $this->configHelper->getSortingIndices($indexName, $storeId);
235+
$indexName = $this->indexNameFetcher->getProductIndexName($storeId);
236+
$sortingIndices = $this->configHelper->getSortingIndices($storeId);
232237
$newMagentoReplicasSetting = $this->transformSortingIndicesToReplicaSetting($sortingIndices);
233238
$oldMagentoReplicasSetting = $this->getMagentoReplicaConfigurationFromAlgolia($indexName);
234239
$nonMagentoReplicasSetting = $this->getNonMagentoReplicaConfigurationFromAlgolia($indexName);
@@ -263,16 +268,16 @@ protected function setReplicasOnPrimaryIndex(string $indexName, int $storeId): a
263268
}
264269

265270
/**
266-
* @param string $primaryIndexName
267271
* @param int $storeId
268272
* @return bool
269273
* @throws LocalizedException
270274
* @throws NoSuchEntityException
271275
* @throws ReplicaLimitExceededException
276+
* @throws TooManyCustomerGroupsAsReplicasException
272277
*/
273-
protected function isReplicaConfigurationValid(string $primaryIndexName, int $storeId): bool
278+
protected function isReplicaConfigurationValid(int $storeId): bool
274279
{
275-
$sortingIndices = $this->configHelper->getSortingIndices($primaryIndexName, $storeId);
280+
$sortingIndices = $this->configHelper->getSortingIndices($storeId);
276281
$validator = $this->validatorFactory->create();
277282
if (!$validator->isReplicaConfigurationValid($sortingIndices)) {
278283
$postfix = "Please note that there can be no more than " . $this->getMaxVirtualReplicasPerIndex() . " virtual replicas per index.";
@@ -347,7 +352,6 @@ protected function deleteReplicas(array $replicasToDelete): void
347352

348353
/**
349354
* Apply ranking settings to the added replica indices
350-
* @param string $primaryIndexName
351355
* @param int $storeId
352356
* @param string[] $replicas
353357
* @param array<string, mixed> $primaryIndexSettings
@@ -356,9 +360,9 @@ protected function deleteReplicas(array $replicasToDelete): void
356360
* @throws LocalizedException
357361
* @throws NoSuchEntityException
358362
*/
359-
protected function configureRanking(string $primaryIndexName, int $storeId, array $replicas, array $primaryIndexSettings): void
363+
protected function configureRanking(int $storeId, array $replicas, array $primaryIndexSettings): void
360364
{
361-
$sortingIndices = $this->configHelper->getSortingIndices($primaryIndexName, $storeId);
365+
$sortingIndices = $this->configHelper->getSortingIndices($storeId);
362366
$replicaDetails = array_filter(
363367
$sortingIndices,
364368
function($replica) use ($replicas) {

0 commit comments

Comments
 (0)