Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reimport: clearing objects in the initial index if the temporary index has zero objects #186

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
21 changes: 15 additions & 6 deletions src/Console/Commands/ReImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,25 @@ public function handle(

$temporaryIndex = $client->initIndex($temporaryName);

try {
$temporaryIndex->getSettings();

$response = $client->moveIndex($temporaryName, $index->getIndexName());
$hits = (int) $temporaryIndex->search('')['nbHits'];

if ($hits === 0) {
$response = $index->clearObjects();
if ($config->get('scout.synchronous', false)) {
$response->wait();
}
} catch (NotFoundException $e) {
$index->setSettings(['attributesForFaceting' => null])->wait();
} else {
try {
$temporaryIndex->getSettings();

$response = $client->moveIndex($temporaryName, $index->getIndexName());

if ($config->get('scout.synchronous', false)) {
$response->wait();
}
} catch (NotFoundException $e) {
$index->setSettings(['attributesForFaceting' => null])->wait();
}
}
}

Expand Down
27 changes: 27 additions & 0 deletions tests/Features/ReimportCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,31 @@ public function testReimport(): void

Artisan::call('scout:reimport', ['searchable' => User::class]);
}

public function testEmptyReimport(): void
{
factory(User::class, 0)->create();

$client = $this->mockClient();

$userIndex = $this->mockIndex(User::class);

$userTemporaryIndex = $this->mockIndex('temp_'.(new User())->searchableAs());

$client->shouldReceive('copyIndex')->with((new User())->searchableAs(), $userTemporaryIndex->getIndexName(), [
'scope' => [
'settings',
'synonyms',
'rules',
],
])->andReturn($this->mockResponse());

$userTemporaryIndex->shouldReceive('search')->andReturn(['nbHits' => 0]);

$userIndex->shouldReceive('clearObjects')->andReturn(['nbHits' => 0]);

$client->shouldReceive('clearObjects')->andReturn($this->mockResponse());

Artisan::call('scout:reimport', ['searchable' => User::class]);
}
}