Skip to content

Commit 3c1799f

Browse files
committed
MAGE-1260: use buildWithComputedIndex rather than buildWithEnforcedIndex when possible
1 parent 16f6126 commit 3c1799f

File tree

11 files changed

+102
-94
lines changed

11 files changed

+102
-94
lines changed

Helper/Entity/ProductHelper.php

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Algolia\AlgoliaSearch\Helper\Entity;
44

5+
use Algolia\AlgoliaSearch\Api\Data\IndexOptionsInterface;
56
use Algolia\AlgoliaSearch\Api\Product\ReplicaManagerInterface;
67
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
78
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
@@ -313,6 +314,7 @@ public function setSettings(string $indexName, string $indexNameTmp, int $storeI
313314
{
314315
$indexSettings = $this->getIndexSettings($storeId);
315316
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexName, $storeId);
317+
$indexTmpOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexNameTmp, $storeId);
316318

317319
$this->algoliaConnector->setSettings(
318320
$indexOptions,
@@ -323,7 +325,6 @@ public function setSettings(string $indexName, string $indexNameTmp, int $storeI
323325

324326
$this->logger->log('Settings: ' . json_encode($indexSettings));
325327
if ($saveToTmpIndicesToo) {
326-
$indexTmpOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexNameTmp, $storeId);
327328

328329
$this->algoliaConnector->setSettings(
329330
$indexTmpOptions,
@@ -336,22 +337,19 @@ public function setSettings(string $indexName, string $indexNameTmp, int $storeI
336337
$this->logger->log('Pushing the same settings to TMP index as well');
337338
}
338339

339-
$this->setFacetsQueryRules($indexName, $storeId);
340+
$this->setFacetsQueryRules($indexOptions);
340341
$this->algoliaConnector->waitLastTask($storeId);
341342

342343
if ($saveToTmpIndicesToo) {
343-
$this->setFacetsQueryRules($indexNameTmp, $storeId);
344+
$this->setFacetsQueryRules($indexTmpOptions);
344345
$this->algoliaConnector->waitLastTask($storeId);
345346
}
346347

347348
$this->replicaManager->syncReplicasToAlgolia($storeId, $indexSettings);
348349

349350
if ($saveToTmpIndicesToo) {
350351
try {
351-
$fromIndexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexName, $storeId);
352-
$toIndexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexNameTmp, $storeId);
353-
354-
$this->algoliaConnector->copySynonyms($fromIndexOptions, $toIndexOptions);
352+
$this->algoliaConnector->copySynonyms($indexOptions, $indexTmpOptions);
355353
$this->algoliaConnector->waitLastTask($storeId);
356354
$this->logger->log('
357355
Copying synonyms from production index to "' . $indexNameTmp . '" to not erase them with the index move.
@@ -361,10 +359,7 @@ public function setSettings(string $indexName, string $indexNameTmp, int $storeI
361359
}
362360

363361
try {
364-
$fromIndexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexName, $storeId);
365-
$toIndexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexNameTmp, $storeId);
366-
367-
$this->algoliaConnector->copyQueryRules($fromIndexOptions, $toIndexOptions);
362+
$this->algoliaConnector->copyQueryRules($indexOptions, $indexTmpOptions);
368363
$this->algoliaConnector->waitLastTask($storeId);
369364
$this->logger->log('
370365
Copying query rules from production index to "' . $indexNameTmp . '" to not erase them with the index move.
@@ -491,17 +486,17 @@ protected function getUnretrieveableAttributes($storeId = null)
491486
}
492487

493488
/**
494-
* @param string $indexName
495-
* @param int|null $storeId
489+
* @param IndexOptionsInterface $indexOptions
496490
* @return void
497491
* @throws AlgoliaException
492+
* @throws NoSuchEntityException
498493
*/
499-
protected function setFacetsQueryRules(string $indexName, int $storeId = null)
494+
protected function setFacetsQueryRules(IndexOptionsInterface $indexOptions)
500495
{
501-
$this->clearFacetsQueryRules($indexName, $storeId);
496+
$this->clearFacetsQueryRules($indexOptions);
502497

503498
$rules = [];
504-
$facets = $this->configHelper->getFacets($storeId);
499+
$facets = $this->configHelper->getFacets($indexOptions->getStoreId());
505500
foreach ($facets as $facet) {
506501
if (!array_key_exists('create_rule', $facet) || $facet['create_rule'] !== '1') {
507502
continue;
@@ -531,23 +526,20 @@ protected function setFacetsQueryRules(string $indexName, int $storeId = null)
531526
}
532527

533528
if ($rules) {
534-
$this->logger->log('Setting facets query rules to "' . $indexName . '" index: ' . json_encode($rules));
529+
$this->logger->log('Setting facets query rules to "' . $indexOptions->getIndexName() . '" index: ' . json_encode($rules));
535530

536-
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexName, $storeId);
537531
$this->algoliaConnector->saveRules($indexOptions, $rules, true);
538532
}
539533
}
540534

541535
/**
542-
* @param $indexName
543-
* @param int|null $storeId
536+
* @param IndexOptionsInterface $indexOptions
544537
* @return void
545538
* @throws AlgoliaException
539+
* @throws NoSuchEntityException
546540
*/
547-
protected function clearFacetsQueryRules($indexName, int $storeId = null): void
541+
protected function clearFacetsQueryRules(IndexOptionsInterface $indexOptions): void
548542
{
549-
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexName, $storeId);
550-
551543
try {
552544
$hitsPerPage = 100;
553545
$page = 0;

Helper/MerchandisingHelper.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
66
use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper;
77
use Algolia\AlgoliaSearch\Service\AlgoliaConnector;
8-
use Algolia\AlgoliaSearch\Service\IndexOptionsBuilder;
8+
use Algolia\AlgoliaSearch\Service\Product\IndexOptionsBuilder;
99

1010
class MerchandisingHelper
1111
{
@@ -37,8 +37,6 @@ public function saveQueryRule(int $storeId,
3737
return;
3838
}
3939

40-
$productsIndexName = $this->productHelper->getIndexName($storeId);
41-
4240
$positions = $this->transformPositions($rawPositions);
4341
$condition = [
4442
'pattern' => '',
@@ -75,7 +73,7 @@ public function saveQueryRule(int $storeId,
7573

7674
// Not catching AlgoliaSearchException for disabled query rules on purpose
7775
// It displays correct error message and navigates user to pricing page
78-
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($productsIndexName, $storeId);
76+
$indexOptions = $this->indexOptionsBuilder->buildEntityIndexOptions($storeId);
7977
$this->algoliaConnector->saveRule($rule, $indexOptions);
8078
}
8179

@@ -92,12 +90,11 @@ public function deleteQueryRule(int $storeId, int $entityId, string $entityType)
9290
return;
9391
}
9492

95-
$productsIndexName = $this->productHelper->getIndexName($storeId);
9693
$ruleId = $this->getQueryRuleId($entityId, $entityType);
9794

9895
// Not catching AlgoliaSearchException for disabled query rules on purpose
9996
// It displays correct error message and navigates user to pricing page
100-
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($productsIndexName, $storeId);
97+
$indexOptions = $this->indexOptionsBuilder->buildEntityIndexOptions($storeId);
10198
$this->algoliaConnector->deleteRule($indexOptions, $ruleId);
10299
}
103100

Model/IndicesConfigurator.php

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Algolia\AlgoliaSearch\Logger\DiagnosticsLogger;
1414
use Algolia\AlgoliaSearch\Service\AlgoliaConnector;
1515
use Algolia\AlgoliaSearch\Service\AlgoliaCredentialsManager;
16-
use Algolia\AlgoliaSearch\Service\IndexNameFetcher;
1716
use Algolia\AlgoliaSearch\Service\IndexOptionsBuilder;
1817
use Magento\Framework\Exception\NoSuchEntityException;
1918

@@ -98,10 +97,12 @@ protected function setCategoriesSettings(int $storeId): void
9897
$logEventName = 'Pushing settings for categories indices.';
9998
$this->logger->start($logEventName, true);
10099

101-
$indexName = $this->categoryHelper->getIndexName($storeId);
102100
$settings = $this->categoryHelper->getIndexSettings($storeId);
103101

104-
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexName, $storeId);
102+
$indexOptions = $this->indexOptionsBuilder->buildWithComputedIndex(
103+
CategoryHelper::INDEX_NAME_SUFFIX,
104+
$storeId
105+
);
105106

106107
$this->algoliaConnector->setSettings(
107108
$indexOptions,
@@ -110,7 +111,7 @@ protected function setCategoriesSettings(int $storeId): void
110111
true
111112
);
112113

113-
$this->logger->log('Index name: ' . $indexName);
114+
$this->logger->log('Index name: ' . $indexOptions->getIndexName());
114115
$this->logger->log('Settings: ' . json_encode($settings));
115116
$this->logger->stop($logEventName, true);
116117
}
@@ -126,9 +127,8 @@ protected function setPagesSettings(int $storeId): void
126127
$this->logger->start($logEventName, true);
127128

128129
$settings = $this->pageHelper->getIndexSettings($storeId);
129-
$indexName = $this->pageHelper->getIndexName($storeId);
130130

131-
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexName, $storeId);
131+
$indexOptions = $this->indexOptionsBuilder->buildWithComputedIndex(PageHelper::INDEX_NAME_SUFFIX, $storeId);
132132

133133
$this->algoliaConnector->setSettings(
134134
$indexOptions,
@@ -137,7 +137,7 @@ protected function setPagesSettings(int $storeId): void
137137
true
138138
);
139139

140-
$this->logger->log('Index name: ' . $indexName);
140+
$this->logger->log('Index name: ' . $indexOptions->getIndexName());
141141
$this->logger->log('Settings: ' . json_encode($settings));
142142
$this->logger->stop($logEventName, true);
143143
}
@@ -152,9 +152,8 @@ protected function setQuerySuggestionsSettings(int $storeId): void
152152
$logEventName = 'Pushing settings for query suggestions indices.';
153153
$this->logger->start($logEventName, true);
154154

155-
$indexName = $this->suggestionHelper->getIndexName($storeId);
156155
$settings = $this->suggestionHelper->getIndexSettings($storeId);
157-
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexName, $storeId);
156+
$indexOptions = $this->indexOptionsBuilder->buildWithComputedIndex(SuggestionHelper::INDEX_NAME_SUFFIX, $storeId);
158157

159158
$this->algoliaConnector->setSettings(
160159
$indexOptions,
@@ -163,7 +162,7 @@ protected function setQuerySuggestionsSettings(int $storeId): void
163162
true
164163
);
165164

166-
$this->logger->log('Index name: ' . $indexName);
165+
$this->logger->log('Index name: ' . $indexOptions->getIndexName());
167166
$this->logger->log('Settings: ' . json_encode($settings));
168167
$this->logger->stop($logEventName, true);
169168
}
@@ -243,19 +242,26 @@ protected function setExtraSettings(int $storeId, bool $saveToTmpIndicesToo): vo
243242
'suggestions' => $this->suggestionHelper->getIndexName($storeId),
244243
'additional_sections' => $this->additionalSectionHelper->getIndexName($storeId)
245244
];
245+
$sections = [
246+
'products',
247+
'categories',
248+
'pages',
249+
'suggestions',
250+
'additional_sections'
251+
];
246252

247253
$error = [];
248-
foreach ($sections as $section => $indexName) {
254+
foreach ($sections as $section) {
249255
try {
250256
$extraSettings = $this->configHelper->getExtraSettings($section, $storeId);
251257

252258
if ($extraSettings) {
253259
$extraSettings = json_decode($extraSettings, true);
254260

255-
$this->logger->log('Index name: ' . $indexName);
256-
$this->logger->log('Extra settings: ' . json_encode($extraSettings));
261+
$indexOptions = $this->indexOptionsBuilder->buildWithComputedIndex('_' . $section, $storeId);
257262

258-
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexName, $storeId);
263+
$this->logger->log('Index name: ' . $indexOptions->getIndexName());
264+
$this->logger->log('Extra settings: ' . json_encode($extraSettings));
259265

260266
$this->algoliaConnector->setSettings(
261267
$indexOptions,
@@ -266,11 +272,14 @@ protected function setExtraSettings(int $storeId, bool $saveToTmpIndicesToo): vo
266272
$this->algoliaConnector->waitLastTask($storeId);
267273

268274
if ($section === 'products' && $saveToTmpIndicesToo) {
269-
$tempIndexName = $indexName . IndexNameFetcher::INDEX_TEMP_SUFFIX;
270-
$this->logger->log('Index name: ' . $tempIndexName);
271-
$this->logger->log('Extra settings: ' . json_encode($extraSettings));
275+
$indexTempOptions = $this->indexOptionsBuilder->buildWithComputedIndex(
276+
ProductHelper::INDEX_NAME_SUFFIX,
277+
$storeId,
278+
true
279+
);
272280

273-
$indexTempOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($tempIndexName, $storeId);
281+
$this->logger->log('Index name: ' . $indexTempOptions->getIndexName());
282+
$this->logger->log('Extra settings: ' . json_encode($extraSettings));
274283

275284
$this->algoliaConnector->setSettings(
276285
$indexTempOptions,

Service/AbstractIndexBuilder.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Algolia\AlgoliaSearch\Service;
44

5+
use Algolia\AlgoliaSearch\Api\Data\IndexOptionsInterface;
56
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
67
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
78
use Algolia\AlgoliaSearch\Logger\DiagnosticsLogger;
@@ -69,33 +70,30 @@ protected function stopEmulation(): void
6970

7071
/**
7172
* @param array $objects
72-
* @param string $indexName
73+
* @param IndexOptionsInterface $indexOptions
7374
* @param int|null $storeId
7475
* @return void
75-
* @throws \Exception
76+
* @throws AlgoliaException
77+
* @throws NoSuchEntityException
7678
*/
77-
protected function saveObjects(array $objects, string $indexName, int $storeId = null): void
79+
protected function saveObjects(array $objects, IndexOptionsInterface $indexOptions): void
7880
{
79-
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexName, $storeId);
80-
8181
$this->algoliaConnector->saveObjects($indexOptions, $objects, $this->configHelper->isPartialUpdateEnabled());
8282
}
8383

8484
/**
85-
* @param $indexName
86-
* @param $idsToRemove
87-
* @param null $storeId
88-
* @return array|mixed
85+
* @param IndexOptionsInterface $indexOptions
86+
* @param array $idsToRemove
87+
* @param int|null $storeId
88+
* @return array
8989
* @throws AlgoliaException
9090
*/
91-
protected function getIdsToRealRemove($indexName, $idsToRemove, $storeId = null)
91+
protected function getIdsToRealRemove(IndexOptionsInterface $indexOptions, array $idsToRemove, ?int $storeId = null)
9292
{
9393
if (count($idsToRemove) === 1) {
9494
return $idsToRemove;
9595
}
9696

97-
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexName, $storeId);
98-
9997
$toRealRemove = [];
10098
$idsToRemove = array_map('strval', $idsToRemove);
10199
foreach (array_chunk($idsToRemove, 1000) as $chunk) {

Service/AdditionalSection/IndexBuilder.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,20 @@ public function buildIndex(int $storeId, ?array $entityIds, ?array $options): vo
7575

7676
$indexName = $this->additionalSectionHelper->getIndexName($storeId);
7777
$indexName = $indexName . '_' . $section['name'];
78+
$tempIndexName = $indexName . IndexNameFetcher::INDEX_TEMP_SUFFIX;
7879

7980
$attributeValues = $this->additionalSectionHelper->getAttributeValues($storeId, $section);
8081

81-
$tempIndexName = $indexName . IndexNameFetcher::INDEX_TEMP_SUFFIX;
82+
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexName, $storeId);
83+
$tempIndexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($tempIndexName, $storeId);
8284

8385
foreach (array_chunk($attributeValues, 100) as $chunk) {
84-
$this->saveObjects($chunk, $tempIndexName, $storeId);
86+
$this->saveObjects($chunk, $tempIndexOptions);
8587
}
8688

87-
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexName, $storeId);
88-
$tempIndexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($tempIndexName, $storeId);
89-
9089
$this->algoliaConnector->copyQueryRules($indexOptions, $tempIndexOptions);
9190
$this->algoliaConnector->moveIndex($tempIndexOptions, $indexOptions);
9291

93-
9492
$this->algoliaConnector->setSettings(
9593
$indexOptions,
9694
$this->additionalSectionHelper->getIndexSettings($storeId)

Service/AlgoliaConnector.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,13 @@ function ($id) {
311311
/**
312312
* Warning: This method can't be performed across two different applications
313313
*
314-
* @param IndexOptions $fromIndexOptions
315-
* @param IndexOptions $toIndexOptions
314+
* @param IndexOptionsInterface $fromIndexOptions
315+
* @param IndexOptionsInterface $toIndexOptions
316316
* @return void
317317
* @throws AlgoliaException
318318
* @throws NoSuchEntityException
319319
*/
320-
public function moveIndex(IndexOptions $fromIndexOptions, IndexOptions $toIndexOptions): void
320+
public function moveIndex(IndexOptionsInterface $fromIndexOptions, IndexOptionsInterface $toIndexOptions): void
321321
{
322322
$fromIndexName = $fromIndexOptions->getIndexName();
323323
$toIndexName = $toIndexOptions->getIndexName();

Service/Category/IndexBuilder.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,17 @@ protected function buildIndexPage($storeId, $collection, $page, $pageSize, $cate
151151
}
152152
$collection->setCurPage($page)->setPageSize($pageSize);
153153
$collection->load();
154-
$indexName = $this->categoryHelper->getIndexName($storeId);
155-
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexName, $storeId);
154+
$indexOptions = $this->indexOptionsBuilder->buildWithComputedIndex(CategoryHelper::INDEX_NAME_SUFFIX, $storeId);
156155
$indexData = $this->getCategoryRecords($storeId, $collection, $categoryIds);
157156
if (!empty($indexData['toIndex'])) {
158157
$this->logger->start('ADD/UPDATE TO ALGOLIA');
159-
$this->saveObjects($indexData['toIndex'], $indexName, $storeId);
158+
$this->saveObjects($indexData['toIndex'], $indexOptions);
160159
$this->logger->log('Product IDs: ' . implode(', ', array_keys($indexData['toIndex'])));
161160
$this->logger->stop('ADD/UPDATE TO ALGOLIA');
162161
}
163162

164163
if (!empty($indexData['toRemove'])) {
165-
$toRealRemove = $this->getIdsToRealRemove($indexName, $indexData['toRemove'], $storeId);
164+
$toRealRemove = $this->getIdsToRealRemove($indexOptions, $indexData['toRemove']);
166165
if (!empty($toRealRemove)) {
167166
$this->logger->start('REMOVE FROM ALGOLIA');
168167
$this->algoliaConnector->deleteObjects($toRealRemove, $indexOptions);

0 commit comments

Comments
 (0)