Skip to content

Commit 87c4183

Browse files
author
Jan Petr
authored
Fix setSettings on TMP index (#785)
Merge settings from production index to TMP index Fixes issue when settings to TMP index were "merged" from non-existing TMP index. Therefore it reset the settings set on production index when TMP index was moved to production as those settings were never set there. Set settings to TMP index immediately before the move Fixes issue when settings were applied to a production index and full reindex process was in progress (TMP index was already existing). Those settings were not propagated to TMP index and during the move of TMP index, the settings were overridden.
1 parent 2685f26 commit 87c4183

File tree

8 files changed

+149
-27
lines changed

8 files changed

+149
-27
lines changed

Helper/AlgoliaHelper.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use AlgoliaSearch\AlgoliaException;
66
use AlgoliaSearch\Client;
77
use AlgoliaSearch\ClientFactory;
8+
use AlgoliaSearch\Index;
89
use AlgoliaSearch\Version;
910
use Magento\Framework\App\Helper\AbstractHelper;
1011
use Magento\Framework\App\Helper\Context;
@@ -126,17 +127,23 @@ public function getObjects($indexName, $objectIds)
126127
* @param $settings
127128
* @param bool $forwardToReplicas
128129
* @param bool $mergeSettings
130+
* @param string $mergeSettingsFrom
129131
*
130132
* @throws AlgoliaException
131133
*/
132-
public function setSettings($indexName, $settings, $forwardToReplicas = false, $mergeSettings = false)
133-
{
134+
public function setSettings(
135+
$indexName,
136+
$settings,
137+
$forwardToReplicas = false,
138+
$mergeSettings = false,
139+
$mergeSettingsFrom = ''
140+
) {
134141
$this->checkClient(__FUNCTION__);
135142

136143
$index = $this->getIndex($indexName);
137144

138145
if ($mergeSettings === true) {
139-
$settings = $this->mergeSettings($indexName, $settings);
146+
$settings = $this->mergeSettings($indexName, $settings, $mergeSettingsFrom);
140147
}
141148

142149
$res = $index->setSettings($settings, $forwardToReplicas);
@@ -185,12 +192,17 @@ public function getSettings($indexName)
185192
return $this->getIndex($indexName)->getSettings();
186193
}
187194

188-
public function mergeSettings($indexName, $settings)
195+
public function mergeSettings($indexName, $settings, $mergeSettingsFrom = '')
189196
{
190197
$onlineSettings = [];
191198

192199
try {
193-
$onlineSettings = $this->getSettings($indexName);
200+
$sourceIndex = $indexName;
201+
if ($mergeSettingsFrom !== '') {
202+
$sourceIndex = $mergeSettingsFrom;
203+
}
204+
205+
$onlineSettings = $this->getSettings($sourceIndex);
194206
} catch (\Exception $e) {
195207
}
196208

@@ -402,6 +414,9 @@ public function waitLastTask($lastUsedIndexName = null, $lastTaskId = null)
402414
{
403415
if ($lastUsedIndexName === null && isset(self::$lastUsedIndexName)) {
404416
$lastUsedIndexName = self::$lastUsedIndexName;
417+
if ($lastUsedIndexName instanceof Index) {
418+
$lastUsedIndexName = $lastUsedIndexName->indexName;
419+
}
405420
}
406421

407422
if ($lastTaskId === null && isset(self::$lastTaskId)) {

Helper/Data.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -258,15 +258,6 @@ public function rebuildStoreSuggestionIndex($storeId)
258258
$this->moveStoreSuggestionIndex($storeId);
259259
}
260260

261-
public function moveIndex($tmpIndexName, $indexName)
262-
{
263-
if ($this->isIndexingEnabled() === false) {
264-
return;
265-
}
266-
267-
$this->algoliaHelper->moveIndex($tmpIndexName, $indexName);
268-
}
269-
270261
public function moveStoreSuggestionIndex($storeId)
271262
{
272263
if ($this->isIndexingEnabled($storeId) === false) {

Helper/Entity/ProductHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ public function setSettings($indexName, $indexNameTmp, $storeId, $saveToTmpIndic
303303
$this->algoliaHelper->setSettings($indexName, $indexSettings, false, true);
304304
$this->logger->log('Settings: ' . json_encode($indexSettings));
305305
if ($saveToTmpIndicesToo === true) {
306-
$this->algoliaHelper->setSettings($indexNameTmp, $indexSettings, false, true);
306+
$this->algoliaHelper->setSettings($indexNameTmp, $indexSettings, false, true, $indexName);
307307
$this->logger->log('Pushing the same settings to TMP index as well');
308308
}
309309

Model/IndexMover.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Model;
4+
5+
use Algolia\AlgoliaSearch\Helper\AlgoliaHelper;
6+
use Algolia\AlgoliaSearch\Helper\Data;
7+
use AlgoliaSearch\AlgoliaException;
8+
9+
class IndexMover
10+
{
11+
/** @var Data */
12+
private $baseHelper;
13+
14+
/** @var AlgoliaHelper */
15+
private $algoliaHelper;
16+
17+
/** @var IndicesConfigurator */
18+
private $indicesConfigurator;
19+
20+
/**
21+
* @param Data $baseHelper
22+
* @param AlgoliaHelper $algoliaHelper
23+
* @param IndicesConfigurator $indicesConfigurator
24+
*/
25+
public function __construct(
26+
Data $baseHelper,
27+
AlgoliaHelper $algoliaHelper,
28+
IndicesConfigurator $indicesConfigurator
29+
) {
30+
$this->baseHelper = $baseHelper;
31+
$this->algoliaHelper = $algoliaHelper;
32+
$this->indicesConfigurator = $indicesConfigurator;
33+
}
34+
35+
/**
36+
* @param string $tmpIndexName
37+
* @param string $indexName
38+
*/
39+
public function moveIndex($tmpIndexName, $indexName)
40+
{
41+
if ($this->baseHelper->isIndexingEnabled() === false) {
42+
return;
43+
}
44+
45+
$this->algoliaHelper->moveIndex($tmpIndexName, $indexName);
46+
}
47+
48+
/**
49+
* @param string $tmpIndexName
50+
* @param string $indexName
51+
* @param int $storeId
52+
*
53+
* @throws AlgoliaException
54+
*/
55+
public function moveIndexWithSetSettings($tmpIndexName, $indexName, $storeId)
56+
{
57+
if ($this->baseHelper->isIndexingEnabled() === false) {
58+
return;
59+
}
60+
61+
$this->indicesConfigurator->saveConfigurationToAlgolia($storeId, true);
62+
$this->moveIndex($tmpIndexName, $indexName);
63+
}
64+
}

Model/Indexer/Product.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
77
use Algolia\AlgoliaSearch\Helper\Data;
88
use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper;
9+
use Algolia\AlgoliaSearch\Model\IndexMover;
910
use Algolia\AlgoliaSearch\Model\IndicesConfigurator;
1011
use Algolia\AlgoliaSearch\Model\Queue;
1112
use Magento;
@@ -119,10 +120,10 @@ public function execute($productIds)
119120
if ($useTmpIndex) {
120121
$suffix = $this->productHelper->getIndexNameSuffix();
121122

122-
/** @uses Data::moveIndex() */
123-
$this->queue->addToQueue(Data::class, 'moveIndex', [
123+
/** @uses IndexMover::moveIndexWithSetSettings() */
124+
$this->queue->addToQueue(IndexMover::class, 'moveIndexWithSetSettings', [
124125
'tmpIndexName' => $this->fullAction->getIndexName($suffix, $storeId, true),
125-
'indexName' => $this->fullAction->getIndexName($suffix, $storeId, false),
126+
'indexName' => $this->fullAction->getIndexName($suffix, $storeId),
126127
'store_id' => $storeId,
127128
], 1, true);
128129
}

Model/Queue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class Queue
6161
/** @var array */
6262
private $staticJobMethods = [
6363
'saveConfigurationToAlgolia',
64-
'moveIndex',
64+
'moveIndexWithSetSettings',
6565
'deleteObjects',
6666
];
6767

Model/Source/JobMethods.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class JobMethods implements \Magento\Framework\Data\OptionSourceInterface
66
{
77
private $methods = [
88
'saveConfigurationToAlgolia' => 'Save Configuration',
9-
'moveIndex' => 'Move Index',
9+
'moveIndexWithSetSettings' => 'Move Index',
1010
'deleteObjects' => 'Object deletion',
1111
'rebuildStoreCategoryIndex' => 'Category Reindex',
1212
'rebuildStoreProductIndex' => 'Product Reindex',

Test/Integration/QueueTest.php

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public function setUp()
3232

3333
public function testFill()
3434
{
35+
$this->resetConfigs([
36+
'algoliasearch_queue/queue/number_of_job_to_run',
37+
'algoliasearch_advanced/advanced/number_of_element_by_page',
38+
]);
39+
3540
$this->setConfig('algoliasearch_queue/queue/active', '1');
3641
$this->connection->query('TRUNCATE TABLE algoliasearch_queue');
3742

@@ -62,7 +67,7 @@ public function testFill()
6267
continue;
6368
}
6469

65-
$this->assertEquals('moveIndex', $row['method']);
70+
$this->assertEquals('moveIndexWithSetSettings', $row['method']);
6671
$this->assertEquals(1, $row['data_size']);
6772
}
6873
}
@@ -120,6 +125,8 @@ public function testExecute()
120125
public function testSettings()
121126
{
122127
$this->resetConfigs([
128+
'algoliasearch_queue/queue/number_of_job_to_run',
129+
'algoliasearch_advanced/advanced/number_of_element_by_page',
123130
'algoliasearch_instant/instant/facets',
124131
'algoliasearch_products/products/product_additional_attributes',
125132
]);
@@ -155,6 +162,45 @@ public function testSettings()
155162
$this->assertFalse(empty($settings['searchableAttributes']), 'SearchableAttributes should be set, but they are not.');
156163
}
157164

165+
public function testMergeSettings()
166+
{
167+
$this->setConfig('algoliasearch_queue/queue/active', '1');
168+
$this->setConfig('algoliasearch_queue/queue/number_of_job_to_run', 1);
169+
$this->setConfig('algoliasearch_advanced/advanced/number_of_element_by_page', 300);
170+
171+
$this->connection->query('TRUNCATE TABLE algoliasearch_queue');
172+
173+
/** @var Product $productIndexer */
174+
$productIndexer = $this->getObjectManager()->create('\Algolia\AlgoliaSearch\Model\Indexer\Product');
175+
$productIndexer->executeFull();
176+
177+
$rows = $this->connection->query('SELECT * FROM algoliasearch_queue')->fetchAll();
178+
$this->assertCount(3, $rows);
179+
180+
$productionIndexName = $this->indexPrefix . 'default_products';
181+
182+
$res = $this->algoliaHelper->getIndex($productionIndexName)->setSettings(['disableTypoToleranceOnAttributes' => ['sku']]);
183+
$this->algoliaHelper->waitLastTask($productionIndexName, $res['taskID']);
184+
185+
$settings = $this->algoliaHelper->getIndex($productionIndexName)->getSettings();
186+
$this->assertEquals(['sku'], $settings['disableTypoToleranceOnAttributes']);
187+
188+
/** @var QueueRunner $queueRunner */
189+
$queueRunner = $this->getObjectManager()->create('\Algolia\AlgoliaSearch\Model\Indexer\QueueRunner');
190+
$queueRunner->executeFull();
191+
192+
$this->algoliaHelper->waitLastTask();
193+
194+
$settings = $this->algoliaHelper->getIndex($this->indexPrefix . 'default_products_tmp')->getSettings();
195+
$this->assertEquals(['sku'], $settings['disableTypoToleranceOnAttributes']);
196+
197+
$queueRunner->executeFull();
198+
$queueRunner->executeFull();
199+
200+
$settings = $this->algoliaHelper->getIndex($productionIndexName)->getSettings();
201+
$this->assertEquals(['sku'], $settings['disableTypoToleranceOnAttributes']);
202+
}
203+
158204
public function testMerging()
159205
{
160206
$this->connection->query('TRUNCATE TABLE algoliasearch_queue');
@@ -452,8 +498,8 @@ public function testMergingWithStaticMethods()
452498
], [
453499
'job_id' => 9,
454500
'pid' => null,
455-
'class' => 'Algolia\AlgoliaSearch\Helper\Data',
456-
'method' => 'moveIndex',
501+
'class' => 'Algolia\AlgoliaSearch\Model\IndexMover',
502+
'method' => 'moveIndexWithSetSettings',
457503
'data' => '{"store_id":"3","category_ids":["40"]}',
458504
'max_retries' => 3,
459505
'retries' => 0,
@@ -472,8 +518,8 @@ public function testMergingWithStaticMethods()
472518
], [
473519
'job_id' => 11,
474520
'pid' => null,
475-
'class' => 'Algolia\AlgoliaSearch\Helper\Data',
476-
'method' => 'moveIndex',
521+
'class' => 'Algolia\AlgoliaSearch\Model\IndexMover',
522+
'method' => 'moveIndexWithSetSettings',
477523
'data' => '{"store_id":"2","product_ids":["405"]}',
478524
'max_retries' => 3,
479525
'retries' => 0,
@@ -511,9 +557,9 @@ public function testMergingWithStaticMethods()
511557
$this->assertEquals('rebuildStoreProductIndex', $jobs[5]->getMethod());
512558
$this->assertEquals('saveConfigurationToAlgolia', $jobs[6]->getMethod());
513559
$this->assertEquals('rebuildStoreCategoryIndex', $jobs[7]->getMethod());
514-
$this->assertEquals('moveIndex', $jobs[8]->getMethod());
560+
$this->assertEquals('moveIndexWithSetSettings', $jobs[8]->getMethod());
515561
$this->assertEquals('rebuildStoreProductIndex', $jobs[9]->getMethod());
516-
$this->assertEquals('moveIndex', $jobs[10]->getMethod());
562+
$this->assertEquals('moveIndexWithSetSettings', $jobs[10]->getMethod());
517563
$this->assertEquals('rebuildStoreProductIndex', $jobs[11]->getMethod());
518564
}
519565

@@ -817,6 +863,11 @@ public function testMaxSingleJobSize()
817863

818864
public function testMaxSingleJobsSizeOnProductReindex()
819865
{
866+
$this->resetConfigs([
867+
'algoliasearch_queue/queue/number_of_job_to_run',
868+
'algoliasearch_advanced/advanced/number_of_element_by_page',
869+
]);
870+
820871
$this->setConfig('algoliasearch_queue/queue/active', '1');
821872

822873
$this->setConfig('algoliasearch_queue/queue/number_of_job_to_run', 10);

0 commit comments

Comments
 (0)