Skip to content

Commit

Permalink
Merge pull request #129 from designmynight/4.2.0
Browse files Browse the repository at this point in the history
Sync 4.2.0 with 4.2.4
  • Loading branch information
jmosul authored Jan 25, 2022
2 parents 1dfa60e + 4921f1c commit 0c7efbf
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 113 deletions.
17 changes: 13 additions & 4 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace DesignMyNight\Elasticsearch;

use Closure;
use DesignMyNight\Elasticsearch\Exceptions\BulkInsertQueryException;
use DesignMyNight\Elasticsearch\Exceptions\QueryException;
use Elasticsearch\ClientBuilder;
use Illuminate\Database\Connection as BaseConnection;
use Illuminate\Database\Events\QueryExecuted;
Expand Down Expand Up @@ -282,17 +284,24 @@ public function scroll(string $scrollId, string $scrollTimeout = '30s', int $lim
/**
* Run an insert statement against the database.
*
* @param array $params
* @param array $bindings
* @param array $params
* @param array $bindings
* @return bool
* @throws BulkInsertQueryException
*/
public function insert($params, $bindings = [])
{
return $this->run(
$result = $this->run(
$this->addClientParams($params),
$bindings,
Closure::fromCallable([$this->connection, 'bulk'])
);

if (!empty($result['errors'])) {
throw new BulkInsertQueryException($result);
}

return true;
}

/**
Expand Down Expand Up @@ -378,7 +387,7 @@ public function prepareBindings(array $bindings)
* @param \Closure $callback
* @return mixed
*
* @throws \DesignMyNight\Elasticsearch\QueryException
* @throws \DesignMyNight\Elasticsearch\Exceptions\QueryException
*/
protected function runQueryCallback($query, $bindings, Closure $callback)
{
Expand Down
61 changes: 61 additions & 0 deletions src/Exceptions/BulkInsertQueryException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace DesignMyNight\Elasticsearch\Exceptions;

use Exception;

class BulkInsertQueryException extends Exception
{
private $errorLimit = 10;

/**
* BulkInsertQueryException constructor.
*
* @param array $queryResult
*/
public function __construct(array $queryResult)
{
parent::__construct($this->formatMessage($queryResult), 400);
}

/**
* Format the error message.
*
* Takes the first {$this->errorLimit} bulk issues and concatenates them to a single string message
*
* @param array $result
* @return string
*/
private function formatMessage(array $result): string
{
$message = [];

$items = array_filter($result['items'] ?? [], function(array $item): bool {
$itemAction = reset($item);

return $itemAction && !empty($itemAction['error']);
});

$items = array_values($items);

$totalErrors = count($items);

// reduce to max limit
$items = array_splice($items, 0, $this->errorLimit);

$message[] = 'Bulk Insert Errors (' . 'Showing ' . count($items) . ' of ' . $totalErrors . '):';

foreach ($items as $item) {
$itemAction = reset($item);

$itemError = array_merge([
'_id' => $itemAction['_id'],
'reason' => $itemAction['error']['reason'],
], $itemAction['error']['caused_by'] ?? []);

$message[] = implode(': ', $itemError);
}

return implode(PHP_EOL, $message);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace DesignMyNight\Elasticsearch;
namespace DesignMyNight\Elasticsearch\Exceptions;

use Illuminate\Database\QueryException as BaseQueryException;

Expand Down
Loading

0 comments on commit 0c7efbf

Please sign in to comment.