From fe3e9a8ec08b00be002248cbf6ea168ccd6a82bb Mon Sep 17 00:00:00 2001 From: Mohammad Ali Tavassoli Date: Tue, 24 Mar 2020 19:08:38 +0430 Subject: [PATCH] Fix delete to use refresh option (#301) --- src/Indexers/BulkIndexer.php | 4 +++ src/Indexers/SingleIndexer.php | 13 +++++--- tests/Indexers/BulkIndexerTest.php | 27 +++++++++++++++++ tests/Indexers/SingleIndexerTest.php | 45 ++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 4 deletions(-) diff --git a/src/Indexers/BulkIndexer.php b/src/Indexers/BulkIndexer.php index 279df26..930f7cf 100644 --- a/src/Indexers/BulkIndexer.php +++ b/src/Indexers/BulkIndexer.php @@ -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()); diff --git a/src/Indexers/SingleIndexer.php b/src/Indexers/SingleIndexer.php index a9094ce..0e99ddd 100644 --- a/src/Indexers/SingleIndexer.php +++ b/src/Indexers/SingleIndexer.php @@ -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()); }); } } diff --git a/tests/Indexers/BulkIndexerTest.php b/tests/Indexers/BulkIndexerTest.php index da735d5..cef28fd 100644 --- a/tests/Indexers/BulkIndexerTest.php +++ b/tests/Indexers/BulkIndexerTest.php @@ -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); + } } diff --git a/tests/Indexers/SingleIndexerTest.php b/tests/Indexers/SingleIndexerTest.php index 37755d7..e91ec17 100644 --- a/tests/Indexers/SingleIndexerTest.php +++ b/tests/Indexers/SingleIndexerTest.php @@ -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); + } }