Skip to content

Commit 1a6d14d

Browse files
authored
Merge pull request #1421 from algolia/feature/MAGE-721
Added option to select replica type per attribute
2 parents 8c687ee + feee7f1 commit 1a6d14d

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

Helper/ConfigHelper.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ class ConfigHelper
143143
public const ENHANCED_QUEUE_ARCHIVE = 'algoliasearch_advanced/queue/enhanced_archive';
144144
public const NUMBER_OF_ELEMENT_BY_PAGE = 'algoliasearch_advanced/queue/number_of_element_by_page';
145145
public const ARCHIVE_LOG_CLEAR_LIMIT = 'algoliasearch_advanced/queue/archive_clear_limit';
146+
public const MAX_VIRTUAL_REPLICA_COUNT = 50;
146147

147148
/**
148149
* @var Magento\Framework\App\Config\ScopeConfigInterface
@@ -1009,7 +1010,14 @@ public function getSortingIndices($originalIndexName, $storeId = null, $currentC
10091010

10101011
$currency = $this->getCurrencyCode($storeId);
10111012
$attributesToAdd = [];
1013+
$defaultVirtualReplicaEnabled = $this->useVirtualReplica($storeId);
1014+
$virtualReplicaCount = 0;
10121015
foreach ($attrs as $key => $attr) {
1016+
if ($virtualReplicaCount < self::MAX_VIRTUAL_REPLICA_COUNT && ($defaultVirtualReplicaEnabled || (isset($attr['virtualReplica']) && $attr['virtualReplica']))){
1017+
$virtualReplica = 1;
1018+
} else {
1019+
$virtualReplica = 0;
1020+
}
10131021
$indexName = false;
10141022
$sortAttribute = false;
10151023
if ($this->isCustomerGroupsEnabled($storeId) && $attr['attribute'] === 'price') {
@@ -1028,6 +1036,7 @@ public function getSortingIndices($originalIndexName, $storeId = null, $currentC
10281036
$newAttr['attribute'] = $attr['attribute'];
10291037
$newAttr['sort'] = $attr['sort'];
10301038
$newAttr['sortLabel'] = $attr['sortLabel'];
1039+
$newAttr['virtualReplica'] = $virtualReplica;
10311040
if (!array_key_exists('label', $newAttr) && array_key_exists('sortLabel', $newAttr)) {
10321041
$newAttr['label'] = $newAttr['sortLabel'];
10331042
}
@@ -1043,6 +1052,7 @@ public function getSortingIndices($originalIndexName, $storeId = null, $currentC
10431052
'custom',
10441053
];
10451054
$attributesToAdd[$newAttr['sort']][] = $newAttr;
1055+
$virtualReplicaCount++;
10461056
}
10471057
} elseif ($attr['attribute'] === 'price') {
10481058
$indexName = $originalIndexName . '_' . $attr['attribute'] . '_' . 'default' . '_' . $attr['sort'];
@@ -1053,6 +1063,7 @@ public function getSortingIndices($originalIndexName, $storeId = null, $currentC
10531063
}
10541064
if ($indexName && $sortAttribute) {
10551065
$attrs[$key]['name'] = $indexName;
1066+
$attrs[$key]['virtualReplica'] = $virtualReplica;
10561067
if (!array_key_exists('label', $attrs[$key]) && array_key_exists('sortLabel', $attrs[$key])) {
10571068
$attrs[$key]['label'] = $attrs[$key]['sortLabel'];
10581069
}
@@ -1067,6 +1078,7 @@ public function getSortingIndices($originalIndexName, $storeId = null, $currentC
10671078
'exact',
10681079
'custom',
10691080
];
1081+
$virtualReplicaCount++;
10701082
}
10711083
}
10721084
$attrsToReturn = [];

Helper/Entity/ProductHelper.php

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -452,12 +452,11 @@ public function setSettings($indexName, $indexNameTmp, $storeId, $saveToTmpIndic
452452
* Handle replicas
453453
*/
454454
$sortingIndices = $this->configHelper->getSortingIndices($indexName, $storeId);
455-
456455
$replicas = [];
457456

458457
if ($this->configHelper->isInstantEnabled($storeId)) {
459458
$replicas = array_values(array_map(function ($sortingIndex) {
460-
return $sortingIndex['name'];
459+
return [$sortingIndex['name'],$sortingIndex['virtualReplica']];
461460
}, $sortingIndices));
462461
}
463462

@@ -483,25 +482,23 @@ public function setSettings($indexName, $indexNameTmp, $storeId, $saveToTmpIndic
483482
$this->logger->log('Setting replicas to "' . $indexName . '" index.');
484483
$this->logger->log('Replicas: ' . json_encode($replicas));
485484
$setReplicasTaskId = $this->algoliaHelper->getLastTaskId();
486-
487-
if (!$this->configHelper->useVirtualReplica($storeId)) {
488-
foreach ($sortingIndices as $values) {
489-
$replicaName = $values['name'];
490-
$indexSettings['ranking'] = $values['ranking'];
491-
$this->algoliaHelper->setSettings($replicaName, $indexSettings, false, true);
492-
$this->logger->log('Setting settings to "' . $replicaName . '" replica.');
493-
$this->logger->log('Settings: ' . json_encode($indexSettings));
494-
}
495-
} else {
496-
foreach ($sortingIndices as $values) {
485+
foreach ($sortingIndices as $values) {
486+
if ($values['virtualReplica']) {
497487
$replicaName = $values['name'];
498488
array_unshift($customRanking, $values['ranking'][0]);
499489
$replicaSetting['customRanking'] = $customRanking;
500490
$this->algoliaHelper->setSettings($replicaName, $replicaSetting, false, false);
501491
$this->logger->log('Setting settings to "' . $replicaName . '" replica.');
502492
$this->logger->log('Settings: ' . json_encode($replicaSetting));
493+
} else {
494+
$replicaName = $values['name'];
495+
$indexSettings['ranking'] = $values['ranking'];
496+
$this->algoliaHelper->setSettings($replicaName, $indexSettings, false, true);
497+
$this->logger->log('Setting settings to "' . $replicaName . '" replica.');
498+
$this->logger->log('Settings: ' . json_encode($indexSettings));
503499
}
504500
}
501+
505502
} else {
506503
$this->algoliaHelper->setSettings($indexName, ['replicas' => []]);
507504
$this->logger->log('Removing replicas from "' . $indexName . '" index');
@@ -574,7 +571,7 @@ public function getObject(Product $product)
574571
);
575572

576573
$defaultData = $transport->getData();
577-
574+
578575
$visibility = $product->getVisibility();
579576

580577
$visibleInCatalog = $this->visibility->getVisibleInCatalogIds();
@@ -1056,7 +1053,7 @@ protected function addAdditionalAttributes($customData, $additionalAttributes, P
10561053
}
10571054

10581055
$attributeResource = $attributeResource->setData('store_id', $product->getStoreId());
1059-
1056+
10601057
$value = $product->getData($attributeName);
10611058

10621059
if ($value !== null) {
@@ -1538,9 +1535,13 @@ public function handleVirtualReplica($replicas)
15381535
{
15391536
$virtualReplicaArray = [];
15401537
foreach ($replicas as $replica) {
1541-
$virtualReplicaArray[] = 'virtual(' . $replica . ')';
1538+
if ($replica[1]) {
1539+
$replicaArray[] = 'virtual(' . $replica[0] . ')';
1540+
} else {
1541+
$replicaArray[] = $replica[0];
1542+
}
15421543
}
1543-
return $virtualReplicaArray;
1544+
return $replicaArray;
15441545
}
15451546

15461547
/**
@@ -1555,8 +1556,9 @@ public function handlingReplica($indexName, $storeId, $sortingAttribute = false)
15551556
$sortingIndices = $this->configHelper->getSortingIndices($indexName, $storeId, null, $sortingAttribute);
15561557
if ($this->configHelper->isInstantEnabled($storeId)) {
15571558
$replicas = array_values(array_map(function ($sortingIndex) {
1558-
return $sortingIndex['name'];
1559+
return [$sortingIndex['name'],$sortingIndex['virtualReplica']];
15591560
}, $sortingIndices));
1561+
15601562
try {
15611563
$replicasFormated = $this->handleVirtualReplica($replicas);
15621564
$availableReplicaMatch = array_merge($replicasFormated, $replicas);
@@ -1584,4 +1586,4 @@ public function handlingReplica($indexName, $storeId, $sortingAttribute = false)
15841586
}
15851587
return true;
15861588
}
1587-
}
1589+
}

Model/Source/Sorts.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ protected function getTableData()
3333
'sortLabel' => [
3434
'label' => 'Label',
3535
],
36+
'virtualReplica' => [
37+
'label' => 'Enable Virtual Replica?',
38+
'values' => ['0' => __('No'), '1' => __('Yes')],
39+
],
3640
];
3741
}
3842
}

0 commit comments

Comments
 (0)