From 30766d6b251f7cec3f9b22742852a2f97788a3df Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Thu, 27 May 2021 10:42:03 -0400 Subject: [PATCH 1/4] Add index error handling --- src/Documents/DocumentManager.php | 25 ++++++++++- src/ElasticException.php | 7 +++ tests/Unit/Documents/DocumentManagerTest.php | 46 ++++++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 src/ElasticException.php diff --git a/src/Documents/DocumentManager.php b/src/Documents/DocumentManager.php index bc32481..9b44223 100644 --- a/src/Documents/DocumentManager.php +++ b/src/Documents/DocumentManager.php @@ -2,6 +2,8 @@ namespace ElasticAdapter\Documents; +use ElasticAdapter\Support\Arr; +use ElasticAdapter\ElasticException; use ElasticAdapter\Search\SearchRequest; use ElasticAdapter\Search\SearchResponse; use Elasticsearch\Client; @@ -44,11 +46,32 @@ public function index( $params['body'][] = $document->getContent(); } - $this->client->bulk($params); + $response = $this->client->bulk($params); + + if ($response['errors'] ?? false) { + $error = $this->getFirstErrorFromResponse($response); + + throw new ElasticException( + sprintf('%s: %s', $error['type'], $error['reason']) + ); + } return $this; } + protected function getFirstErrorFromResponse(array $response) + { + foreach ($response['items'] as $item) { + $result = reset($item); + + if (! isset($result['error'])) { + continue; + } + + return $result['error']; + } + } + /** * @param Document[] $documents */ diff --git a/src/ElasticException.php b/src/ElasticException.php new file mode 100644 index 0000000..fdd6541 --- /dev/null +++ b/src/ElasticException.php @@ -0,0 +1,7 @@ +assertSame(1, $response->getHitsTotal()); $this->assertEquals(new Document('1', ['content' => 'foo']), $response->getHits()[0]->getDocument()); } + + public function test_exception_is_thrown_when_index_response_contains_error(): 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' => [ + [ + 'index' => [ + '_index' => 'test', + '_type' => '_doc', + '_id' => '1', + 'status' => 400, + 'error' => [ + 'type' => "mapper_parsing_exception", + 'reason' => "failed to parse field [title] of type [text] in document with id '1'. Preview of field's value: 'Doc 1'", + 'caused_by' => [ + 'type' => 'illegal_state_exception', + 'reason' => "Can't get text on a START_OBJECT at 1:362", + ], + ], + ], + ], + ], + ]); + + $this->expectException(ElasticException::class); + $this->expectExceptionMessage( + "mapper_parsing_exception: failed to parse field [title] of type [text] in document with id '1'. Preview of field's value: 'Doc 1'" + ); + + $this->documentManager->index('test', [ + new Document('1', ['title' => 'Doc 1']), + ], false); + } } From 6e6a5b941723c7c1a852d7ff258cf958de0e8437 Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Thu, 27 May 2021 10:51:48 -0400 Subject: [PATCH 2/4] Remove unused import --- src/Documents/DocumentManager.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Documents/DocumentManager.php b/src/Documents/DocumentManager.php index 9b44223..50ad5f4 100644 --- a/src/Documents/DocumentManager.php +++ b/src/Documents/DocumentManager.php @@ -2,7 +2,6 @@ namespace ElasticAdapter\Documents; -use ElasticAdapter\Support\Arr; use ElasticAdapter\ElasticException; use ElasticAdapter\Search\SearchRequest; use ElasticAdapter\Search\SearchResponse; From 90470211ba1bdb1245906f4c99b098f0ea43645e Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Thu, 27 May 2021 12:55:51 -0400 Subject: [PATCH 3/4] Add ability to get indexes --- src/Indices/IndexManager.php | 7 +++++++ tests/Unit/Indices/IndexManagerTest.php | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/Indices/IndexManager.php b/src/Indices/IndexManager.php index 11679f4..2be55e6 100644 --- a/src/Indices/IndexManager.php +++ b/src/Indices/IndexManager.php @@ -35,6 +35,13 @@ public function close(string $indexName): self return $this; } + public function get(string $indexPattern): array + { + return $this->indices->get([ + 'index' => $indexPattern, + ]); + } + public function exists(string $indexName): bool { return $this->indices->exists([ diff --git a/tests/Unit/Indices/IndexManagerTest.php b/tests/Unit/Indices/IndexManagerTest.php index de92d92..8dc45d4 100644 --- a/tests/Unit/Indices/IndexManagerTest.php +++ b/tests/Unit/Indices/IndexManagerTest.php @@ -74,6 +74,27 @@ public function test_index_can_be_closed(): void $this->assertSame($this->indexManager, $this->indexManager->close($indexName)); } + public function test_indexes_can_be_retrieved(): void + { + $indexPattern = '*'; + + $this->indices + ->expects($this->once()) + ->method('get') + ->with([ + 'index' => $indexPattern, + ]) + ->willReturn([ + '.kibana' => ['...'], + 'index' => ['...'], + ]); + + $this->assertEquals([ + '.kibana' => ['...'], + 'index' => ['...'], + ], $this->indexManager->get($indexPattern)); + } + public function test_index_existence_can_be_checked(): void { $indexName = 'foo'; From 8183f0d05ea8bdb2c97e34efc804bf20987ee6aa Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Thu, 27 May 2021 12:58:00 -0400 Subject: [PATCH 4/4] Rollback accidental push --- src/Indices/IndexManager.php | 9 +-------- tests/Unit/Indices/IndexManagerTest.php | 21 --------------------- 2 files changed, 1 insertion(+), 29 deletions(-) diff --git a/src/Indices/IndexManager.php b/src/Indices/IndexManager.php index 2be55e6..0a3f12a 100644 --- a/src/Indices/IndexManager.php +++ b/src/Indices/IndexManager.php @@ -34,14 +34,7 @@ public function close(string $indexName): self return $this; } - - public function get(string $indexPattern): array - { - return $this->indices->get([ - 'index' => $indexPattern, - ]); - } - + public function exists(string $indexName): bool { return $this->indices->exists([ diff --git a/tests/Unit/Indices/IndexManagerTest.php b/tests/Unit/Indices/IndexManagerTest.php index 8dc45d4..de92d92 100644 --- a/tests/Unit/Indices/IndexManagerTest.php +++ b/tests/Unit/Indices/IndexManagerTest.php @@ -74,27 +74,6 @@ public function test_index_can_be_closed(): void $this->assertSame($this->indexManager, $this->indexManager->close($indexName)); } - public function test_indexes_can_be_retrieved(): void - { - $indexPattern = '*'; - - $this->indices - ->expects($this->once()) - ->method('get') - ->with([ - 'index' => $indexPattern, - ]) - ->willReturn([ - '.kibana' => ['...'], - 'index' => ['...'], - ]); - - $this->assertEquals([ - '.kibana' => ['...'], - 'index' => ['...'], - ], $this->indexManager->get($indexPattern)); - } - public function test_index_existence_can_be_checked(): void { $indexName = 'foo';