Skip to content
This repository has been archived by the owner on Nov 4, 2021. It is now read-only.

Commit

Permalink
Fix delete to use refresh option (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtvs authored Mar 24, 2020
1 parent c629b3e commit fe3e9a8
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/Indexers/BulkIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public function delete(Collection $models)
$bulkPayload->add('body', $actionPayload->get());
});

if ($documentRefresh = config('scout_elastic.document_refresh')) {
$bulkPayload->set('refresh', $documentRefresh);
}

$bulkPayload->set('client.ignore', 404);

ElasticClient::bulk($bulkPayload->get());
Expand Down
13 changes: 9 additions & 4 deletions src/Indexers/SingleIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,16 @@ public function update(Collection $models)
public function delete(Collection $models)
{
$models->each(function ($model) {
$payload = (new DocumentPayload($model))
->set('client.ignore', 404)
->get();
$payload = new DocumentPayload($model);


if ($documentRefresh = config('scout_elastic.document_refresh')) {
$payload->set('refresh', $documentRefresh);
}

$payload->set('client.ignore', 404);

ElasticClient::delete($payload);
ElasticClient::delete($payload->get());
});
}
}
27 changes: 27 additions & 0 deletions tests/Indexers/BulkIndexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,31 @@ public function testDelete()

$this->addToAssertionCount(1);
}

public function testDeleteWithSpecifiedDocumentRefreshOption()
{
Config::set('scout_elastic.document_refresh', true);

ElasticClient
::shouldReceive('bulk')
->once()
->with([
'index' => 'test',
'type' => 'test',
'body' => [
['delete' => ['_id' => 1]],
['delete' => ['_id' => 2]],
['delete' => ['_id' => 3]],
],
'refresh' => true,
'client' => [
'ignore' => 404,
],
]);

(new BulkIndexer())
->delete($this->models);

$this->addToAssertionCount(1);
}
}
45 changes: 45 additions & 0 deletions tests/Indexers/SingleIndexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,49 @@ public function testDelete()

$this->addToAssertionCount(1);
}

public function testDeleteWithSpecifiedDocumentRefreshOption()
{
Config::set('scout_elastic.document_refresh', true);

ElasticClient
::shouldReceive('delete')
->once()
->with([
'index' => 'test',
'type' => 'test',
'id' => 1,
'refresh' => true,
'client' => [
'ignore' => 404,
],
])
->shouldReceive('delete')
->once()
->with([
'index' => 'test',
'type' => 'test',
'id' => 2,
'refresh' => true,
'client' => [
'ignore' => 404,
],
])
->shouldReceive('delete')
->once()
->with([
'index' => 'test',
'type' => 'test',
'id' => 3,
'refresh' => true,
'client' => [
'ignore' => 404,
],
]);

(new SingleIndexer())
->delete($this->models);

$this->addToAssertionCount(1);
}
}

0 comments on commit fe3e9a8

Please sign in to comment.