Skip to content
This repository was archived by the owner on Jul 31, 2018. It is now read-only.

Commit 144e0bf

Browse files
author
Jan Petr
authored
Merge pull request #77 from algolia/fix_synonyms_with_delete
Fix setSettings with synonyms (with clearSynonyms)
2 parents a3dafd1 + 15a6cac commit 144e0bf

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/AlgoliaEloquentTrait.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,18 @@ public function _setSettings($setToTmpIndices = false)
173173
if (isset($settings['synonyms'])) {
174174
$index->batchSynonyms($settings['synonyms'], true, true);
175175
}
176+
else {
177+
// If no synonyms are passed, clear all synonyms from index
178+
$index->clearSynonyms(true);
179+
}
176180

177181
if (count(array_keys($settings)) > 0) {
178-
$index->setSettings($settings);
182+
// Synonyms cannot be pushed into "setSettings", it's got rejected from API and throwing exception
183+
// Synonyms cannot be removed directly from $settings var, because then synonym would not be set to other indices
184+
$settingsWithoutSynonyms = $settings;
185+
unset($settingsWithoutSynonyms['synonyms']);
186+
187+
$index->setSettings($settingsWithoutSynonyms);
179188
}
180189

181190
if ($b && isset($settings['slaves'])) {
@@ -189,6 +198,7 @@ public function _setSettings($setToTmpIndices = false)
189198
$index = $modelHelper->getIndices($this, $slave)[0];
190199

191200
$s = array_merge($settings, $slaves_settings[$slave]);
201+
unset($s['synonyms']);
192202

193203
if (count(array_keys($s)) > 0)
194204
$index->setSettings($s);

tests/AlgoliaEloquentTraitTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public function testSetSettings()
7070
$index = Mockery::mock('\AlgoliaSearch\Index');
7171
$index->shouldReceive('setSettings')->with(['slaves' => ['model_6_desc_testing']]);
7272
$index->shouldReceive('setSettings')->with(['ranking' => ['desc(name)']]);
73+
$index->shouldReceive('clearSynonyms');
7374

7475
/** @var \AlgoliaSearch\Laravel\ModelHelper $realModelHelper */
7576
$realModelHelper = App::make('\AlgoliaSearch\Laravel\ModelHelper');

0 commit comments

Comments
 (0)