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

Throw exception if any record fails and Log the response #213

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/Indexers/BulkIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
class BulkIndexer implements IndexerInterface
{
/**
* {@inheritdoc}
* @param Collection $models
*
* @return array|void
* @throws \Exception
*/
public function update(Collection $models)
{
Expand Down Expand Up @@ -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');
}
}

/**
Expand Down