Skip to content

Commit

Permalink
Merge pull request #115 from designmynight/4.3.5
Browse files Browse the repository at this point in the history
4.3.5
  • Loading branch information
AndersonSean authored Jul 2, 2021
2 parents 5717061 + b7bdd7c commit e31e678
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
23 changes: 13 additions & 10 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -607,7 +610,7 @@ protected function getDefaultQueryGrammar()
*
* @return mixed
*
* @throws \DesignMyNight\Elasticsearch\QueryException
* @throws QueryException
*/
protected function runQueryCallback($query, $bindings, Closure $callback)
{
Expand Down
14 changes: 9 additions & 5 deletions src/Exceptions/BulkInsertQueryException.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,27 @@ 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);

$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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit e31e678

Please sign in to comment.