Skip to content

Commit

Permalink
Throw an exception when one or more bulk operations were unsuccessful
Browse files Browse the repository at this point in the history
  • Loading branch information
babenkoivan committed Jun 3, 2021
1 parent d3b5daa commit e4a2a64
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/Documents/DocumentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ElasticAdapter\Documents;

use ElasticAdapter\Exceptions\BulkRequestException;
use ElasticAdapter\Search\SearchRequest;
use ElasticAdapter\Search\SearchResponse;
use Elasticsearch\Client;
Expand Down Expand Up @@ -44,7 +45,11 @@ public function index(
$params['body'][] = $document->getContent();
}

$this->client->bulk($params);
$response = $this->client->bulk($params);

if ($response['errors']) {
throw new BulkRequestException($response);
}

return $this;
}
Expand Down Expand Up @@ -74,7 +79,11 @@ public function delete(
$params['body'][] = compact('delete');
}

$this->client->bulk($params);
$response = $this->client->bulk($params);

if ($response['errors']) {
throw new BulkRequestException($response);
}

return $this;
}
Expand Down
24 changes: 24 additions & 0 deletions src/Exceptions/BulkRequestException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);

namespace ElasticAdapter\Exceptions;

use ErrorException;

final class BulkRequestException extends ErrorException
{
/**
* @var array
*/
private $response;

public function __construct(array $response)
{
parent::__construct('One or more operations in the bulk request did not complete successfully');
$this->response = $response;
}

public function getResponse(): array
{
return $this->response;
}
}
60 changes: 59 additions & 1 deletion tests/Unit/Documents/DocumentManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ElasticAdapter\Documents\Document;
use ElasticAdapter\Documents\DocumentManager;
use ElasticAdapter\Exceptions\BulkRequestException;
use ElasticAdapter\Search\SearchRequest;
use ElasticAdapter\Search\SearchResponse;
use Elasticsearch\Client;
Expand All @@ -14,11 +15,12 @@
/**
* @covers \ElasticAdapter\Documents\DocumentManager
*
* @uses \ElasticAdapter\Support\Arr
* @uses \ElasticAdapter\Documents\Document
* @uses \ElasticAdapter\Exceptions\BulkRequestException
* @uses \ElasticAdapter\Search\Hit
* @uses \ElasticAdapter\Search\SearchRequest
* @uses \ElasticAdapter\Search\SearchResponse
* @uses \ElasticAdapter\Support\Arr
*/
final class DocumentManagerTest extends TestCase
{
Expand Down Expand Up @@ -53,6 +55,11 @@ public function test_documents_can_be_indexed_with_refresh(): void
['index' => ['_id' => '2']],
['title' => 'Doc 2'],
],
])
->willReturn([
'took' => 0,
'errors' => false,
'items' => [],
]);

$this->assertSame($this->documentManager, $this->documentManager->index('test', [
Expand All @@ -73,6 +80,11 @@ public function test_documents_can_be_indexed_without_refresh(): void
['index' => ['_id' => '1']],
['title' => 'Doc 1'],
],
])
->willReturn([
'took' => 0,
'errors' => false,
'items' => [],
]);

$this->assertSame($this->documentManager, $this->documentManager->index('test', [
Expand All @@ -94,6 +106,11 @@ public function test_documents_can_be_indexed_with_custom_routing(): void
['index' => ['_id' => '2', 'routing' => 'Doc 2']],
['title' => 'Doc 2'],
],
])
->willReturn([
'took' => 0,
'errors' => false,
'items' => [],
]);

$this->assertSame($this->documentManager, $this->documentManager->index('test', [
Expand All @@ -114,6 +131,11 @@ public function test_documents_can_be_deleted_with_refresh(): void
['delete' => ['_id' => '1']],
['delete' => ['_id' => '2']],
],
])
->willReturn([
'took' => 0,
'errors' => false,
'items' => [],
]);

$this->assertSame($this->documentManager, $this->documentManager->delete('test', [
Expand All @@ -133,6 +155,11 @@ public function test_documents_can_be_deleted_without_refresh(): void
'body' => [
['delete' => ['_id' => '1']],
],
])
->willReturn([
'took' => 0,
'errors' => false,
'items' => [],
]);

$this->assertSame($this->documentManager, $this->documentManager->delete('test', [
Expand All @@ -152,6 +179,11 @@ public function test_documents_can_be_deleted_with_custom_routing(): void
['delete' => ['_id' => '1', 'routing' => 'Doc 1']],
['delete' => ['_id' => '2', 'routing' => 'Doc 2']],
],
])
->willReturn([
'took' => 0,
'errors' => false,
'items' => [],
]);

$this->assertSame($this->documentManager, $this->documentManager->delete('test', [
Expand Down Expand Up @@ -235,4 +267,30 @@ public function test_documents_can_be_found(): void
$this->assertSame(1, $response->getHitsTotal());
$this->assertEquals(new Document('1', ['content' => 'foo']), $response->getHits()[0]->getDocument());
}

public function test_exception_is_thrown_when_index_operation_was_unsuccessful(): void
{
$this->client
->expects($this->once())
->method('bulk')
->with([
'index' => 'test',
'refresh' => 'false',
'body' => [
['index' => ['_id' => '1']],
['title' => 'Doc 1'],
],
])
->willReturn([
'took' => 0,
'errors' => true,
'items' => [],
]);

$this->expectException(BulkRequestException::class);

$this->documentManager->index('test', [
new Document('1', ['title' => 'Doc 1']),
]);
}
}
41 changes: 41 additions & 0 deletions tests/Unit/Exceptions/BulkRequestExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php declare(strict_types=1);

namespace ElasticAdapter\Tests\Unit\Exceptions;

use ElasticAdapter\Exceptions\BulkRequestException;
use PHPUnit\Framework\TestCase;

/**
* @covers \ElasticAdapter\Exceptions\BulkRequestException
*/
final class BulkRequestExceptionTest extends TestCase
{
public function test_response_can_be_retrieved(): void
{
$response = [
'took' => 486,
'errors' => true,
'items' => [
[
'update' => [
'_index' => 'index1',
'_type' => '_doc',
'_id' => '5',
'status' => 404,
'error' => [
'type' => 'document_missing_exception',
'reason' => '[_doc][5]: document missing',
'index_uuid' => 'aAsFqTI0Tc2W0LCWgPNrOA',
'shard' => '0',
'index' => 'index1',
],
],
],
],
];

$exception = new BulkRequestException($response);

$this->assertSame($response, $exception->getResponse());
}
}

0 comments on commit e4a2a64

Please sign in to comment.