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

Commit

Permalink
Merge pull request #77 from algolia/fix_synonyms_with_delete
Browse files Browse the repository at this point in the history
Fix setSettings with synonyms (with clearSynonyms)
  • Loading branch information
Jan Petr authored Aug 22, 2016
2 parents a3dafd1 + 15a6cac commit 144e0bf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/AlgoliaEloquentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,18 @@ public function _setSettings($setToTmpIndices = false)
if (isset($settings['synonyms'])) {
$index->batchSynonyms($settings['synonyms'], true, true);
}
else {
// If no synonyms are passed, clear all synonyms from index
$index->clearSynonyms(true);
}

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

$index->setSettings($settingsWithoutSynonyms);
}

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

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

if (count(array_keys($s)) > 0)
$index->setSettings($s);
Expand Down
1 change: 1 addition & 0 deletions tests/AlgoliaEloquentTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function testSetSettings()
$index = Mockery::mock('\AlgoliaSearch\Index');
$index->shouldReceive('setSettings')->with(['slaves' => ['model_6_desc_testing']]);
$index->shouldReceive('setSettings')->with(['ranking' => ['desc(name)']]);
$index->shouldReceive('clearSynonyms');

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

0 comments on commit 144e0bf

Please sign in to comment.