From 13796d44ff3ecb74bc62001c44ee25b4ad4d7d98 Mon Sep 17 00:00:00 2001 From: Aaron Lee Kafton Date: Thu, 18 Apr 2019 11:12:05 -0500 Subject: [PATCH] Throw exception if any record fails and Log the response for that record --- src/Indexers/BulkIndexer.php | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Indexers/BulkIndexer.php b/src/Indexers/BulkIndexer.php index a2b92fa..6e119a2 100644 --- a/src/Indexers/BulkIndexer.php +++ b/src/Indexers/BulkIndexer.php @@ -11,7 +11,10 @@ class BulkIndexer implements IndexerInterface { /** - * {@inheritdoc} + * @param Collection $models + * + * @return array|void + * @throws \Exception */ public function update(Collection $models) { @@ -50,7 +53,20 @@ public function update(Collection $models) ->add('body', $modelData); }); - ElasticClient::bulk($bulkPayload->get()); + $response = ElasticClient::bulk($bulkPayload->get()); + + if ($response['errors']) { + + // Response included every record's status which is a lot to dig through when chunking by thousand + // Sort through the items to only log the failed items + foreach ($response['items'] as $item) { + if ($item['index']['error']) { + Log::error($item); + } + } + + throw new Exception('ElasticSearch responded with an error'); + } } /**