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

Commit

Permalink
Add option not to erase dashboard settings when reindexing
Browse files Browse the repository at this point in the history
  • Loading branch information
maxiloc committed Nov 7, 2016
1 parent bdd6425 commit 90ba259
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/AlgoliaEloquentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ trait AlgoliaEloquentTrait
*
* @param bool $safe
* @param bool $setSettings
* @param bool $mergeOldSettings
*/
public function _reindex($safe = true, $setSettings = true)
public function _reindex($safe = true, $setSettings = true, $mergeOldSettings = false)
{
/** @var \AlgoliaSearch\Laravel\ModelHelper $modelHelper */
$modelHelper = App::make('\AlgoliaSearch\Laravel\ModelHelper');
Expand All @@ -27,7 +28,7 @@ public function _reindex($safe = true, $setSettings = true)

if ($setSettings === true) {
$setToTmpIndices = ($safe === true);
$this->_setSettings($setToTmpIndices);
$this->_setSettings($setToTmpIndices, $mergeOldSettings);
}

static::chunk(100, function ($models) use ($indicesTmp, $modelHelper) {
Expand Down Expand Up @@ -142,7 +143,7 @@ public function _search($query, $parameters = [])
return $result;
}

public function _setSettings($setToTmpIndices = false)
public function _setSettings($setToTmpIndices = false, $mergeOldSettings = false)
{
/** @var \AlgoliaSearch\Laravel\ModelHelper $modelHelper */
$modelHelper = App::make('\AlgoliaSearch\Laravel\ModelHelper');
Expand All @@ -167,7 +168,29 @@ public function _setSettings($setToTmpIndices = false)
$b = true;

/** @var \AlgoliaSearch\Index $index */
foreach ($indices as $index) {
foreach ($indices as $key => $index) {
if ($mergeOldSettings) {
$old_indices = $modelHelper->getIndices($this);
$old_index = $old_indices[$key];

try {
$oldSettings = $old_index->getSettings();
}
catch (\Exception $e) {
$oldSettings = [];
}

unset($oldSettings['replicas']);
unset($oldSettings['slaves']);

$newSettings = $oldSettings;

foreach ($settings as $settingName => $settingValue) {
$newSettings[$settingName] = $settingValue;
}

$settings = $newSettings;
}

if ($b && isset($settings['replicas'])) {
$settings['replicas'] = array_map(function ($indexName) use ($modelHelper) {
Expand Down
43 changes: 43 additions & 0 deletions tests/AlgoliaEloquentTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use AlgoliaSearch\Tests\Models\Model11;
use AlgoliaSearch\Tests\Models\Model12;
use AlgoliaSearch\Tests\Models\Model13;
use AlgoliaSearch\Tests\Models\Model2;
use AlgoliaSearch\Tests\Models\Model4;
use AlgoliaSearch\Tests\Models\Model6;
Expand Down Expand Up @@ -98,6 +99,48 @@ public function testSetSettings()
$this->assertEquals($modelHelper->getFinalIndexName($model12, $realModelHelper->getSettings($model12)['slaves'][0]), 'model_6_desc_testing');

$model12->setSettings();


}

public function testSetSettingsMerge()
{
$index = Mockery::mock('\AlgoliaSearch\Index');
$index->shouldReceive('getSettings')->andReturn([
'attributesToIndex' => [
'attribute1',
'attribute2',
]
]);
$index->shouldReceive('setSettings')->with([
'attributesToIndex' => [
'attribute1',
'attribute2',
],
'attributesForFaceting' => [
'attribute1',
'attribute2'
]
]);
$index->shouldReceive('clearSynonyms');

/** @var \AlgoliaSearch\Laravel\ModelHelper $realModelHelper */
$realModelHelper = App::make('\AlgoliaSearch\Laravel\ModelHelper');
$modelHelper = Mockery::mock('\AlgoliaSearch\Laravel\ModelHelper');

App::instance('\AlgoliaSearch\Laravel\ModelHelper', $modelHelper);

$model13 = new Model13();

$modelHelper->shouldReceive('getSettings')->andReturn($realModelHelper->getSettings($model13));
$modelHelper->shouldReceive('getIndices')->andReturn([$index]);
$modelHelper->shouldReceive('getFinalIndexName')->andReturn($realModelHelper->getFinalIndexName($model13, 'model_6_desc'));
$modelHelper->shouldReceive('getReplicasSettings')->andReturn($realModelHelper->getReplicasSettings($model13));

// remove warning test is done with to should receive with
$this->assertEquals(1, 1);

$model13->setSettings(false, true);
}

public function testSetSynonyms()
Expand Down
18 changes: 18 additions & 0 deletions tests/Models/Model13.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace AlgoliaSearch\Tests\Models;

use AlgoliaSearch\Laravel\AlgoliaEloquentTrait;
use Illuminate\Database\Eloquent\Model;

class Model13 extends Model
{
use AlgoliaEloquentTrait;

public $algoliaSettings = [
'attributesForFaceting' => [
'attribute1',
'attribute2'
]
];
}

0 comments on commit 90ba259

Please sign in to comment.