diff --git a/composer.json b/composer.json index 9704ca5..33696a5 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "require": { "php": "^7.1", "elasticsearch/elasticsearch": "^5.2", - "laravel/framework": "^5.7" + "laravel/framework": "^6.0" }, "require-dev": { "phpunit/phpunit": "^7.1", diff --git a/src/Connection.php b/src/Connection.php index 92281bf..6fc57db 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -6,12 +6,12 @@ use DesignMyNight\Elasticsearch\Database\Schema\Blueprint; use DesignMyNight\Elasticsearch\Database\Schema\ElasticsearchBuilder; use DesignMyNight\Elasticsearch\Database\Schema\Grammars\ElasticsearchGrammar; +use DesignMyNight\Elasticsearch\Exceptions\BulkInsertQueryException; +use DesignMyNight\Elasticsearch\Exceptions\QueryException; use Elasticsearch\ClientBuilder; use Illuminate\Database\Connection as BaseConnection; use Illuminate\Database\Events\QueryExecuted; use Illuminate\Database\Grammar as BaseGrammar; -use Illuminate\Database\QueryException; -use Illuminate\Database\Schema\Builder; use Illuminate\Support\Arr; class Connection extends BaseConnection @@ -233,20 +233,26 @@ public function getTablePrefix() } /** - * Run an insert statement against the database. + * Run a select statement against the database. * * @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; } /** @@ -355,10 +361,7 @@ public function scroll(string $scrollId, string $scrollTimeout = '30s', int $lim } /** - * Run a select statement against the database. - * - * @param array $params - * @param array $bindings + * Run an update statement against the database. * * @return array */ @@ -607,7 +610,7 @@ protected function getDefaultQueryGrammar() * * @return mixed * - * @throws \DesignMyNight\Elasticsearch\QueryException + * @throws QueryException */ protected function runQueryCallback($query, $bindings, Closure $callback) { diff --git a/src/Exceptions/BulkInsertQueryException.php b/src/Exceptions/BulkInsertQueryException.php index c11b181..9e6ab64 100644 --- a/src/Exceptions/BulkInsertQueryException.php +++ b/src/Exceptions/BulkInsertQueryException.php @@ -31,7 +31,9 @@ private function formatMessage(array $result): string $message = []; $items = array_filter($result['items'] ?? [], function(array $item): bool { - return $item['index'] && !empty($item['index']['error']); + $itemAction = reset($item); + + return $itemAction && !empty($itemAction['error']); }); $items = array_values($items); @@ -39,15 +41,17 @@ private function formatMessage(array $result): string $totalErrors = count($items); // reduce to max limit - array_splice($items, 0, $this->errorLimit); + $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' => $item['_id'], - 'reason' => $item['error']['reason'], - ], $item['error']['caused_by'] ?? []); + '_id' => $itemAction['_id'], + 'reason' => $itemAction['error']['reason'], + ], $itemAction['error']['caused_by'] ?? []); $message[] = implode(': ', $itemError); } diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index f6274f6..9d85716 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -734,7 +734,7 @@ public function cursor() /** * @inheritdoc */ - public function insert(array $values): bool + public function insert(array $values) { // Since every insert gets treated like a batch insert, we will have to detect // if the user is inserting a single document or an array of documents.