Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On client error throw, catch and handle exception #38

Open
wants to merge 7 commits into
base: feature/TAO-10203-advanced-search
Choose a base branch
from
Open
Changes from 3 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
7 changes: 6 additions & 1 deletion src/ElasticSearch.php
Original file line number Diff line number Diff line change
@@ -31,6 +31,7 @@
use oat\tao\model\search\SyntaxException;
use oat\tao\model\search\ResultSet;
use oat\oatbox\service\ConfigurableService;
use common_report_Report;
bartlomiejmarszal marked this conversation as resolved.
Show resolved Hide resolved

/**
* Class ElasticSearch
@@ -131,7 +132,11 @@ public function index($documents = []): int
? $documents
: new ArrayIterator($documents);

return $this->getIndexer()->buildIndex($documents);
try {
return $this->getIndexer()->buildIndex($documents);
} catch (ClientErrorResponseException $exception) {
return common_report_Report::createFailure($exception->getMessage());
bartlomiejmarszal marked this conversation as resolved.
Show resolved Hide resolved
}
}

public function remove($resourceId): bool
26 changes: 23 additions & 3 deletions src/ElasticSearchIndexer.php
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@
namespace oat\tao\elasticsearch;

use Elasticsearch\Client;
use Elasticsearch\Common\Exceptions\ClientErrorResponseException;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Iterator;
@@ -101,7 +102,6 @@ public function buildIndex(Iterator $documents): int

$params = $this->extendBatch('delete', $indexName, $document, $params);
$params = $this->extendBatch('create', $indexName, $document, $params);
$params = $this->extendBatch('update', $indexName, $document, $params);

$documents->next();

@@ -110,6 +110,10 @@ public function buildIndex(Iterator $documents): int
if ($blockSize === self::INDEXING_BLOCK_SIZE) {
$clientResponse = $this->client->bulk($params);

if ($clientResponse['errors'] === true) {
throw new ClientErrorResponseException($this->parseErrors($clientResponse));
}
bartlomiejmarszal marked this conversation as resolved.
Show resolved Hide resolved

$this->logger->debug('client response: '. json_encode($clientResponse));

$count += $blockSize;
@@ -121,7 +125,11 @@ public function buildIndex(Iterator $documents): int
if ($blockSize > 0) {
$clientResponse = $this->client->bulk($params);

$this->logger->debug('client response: '. json_encode($clientResponse));
if ($clientResponse['errors'] === true) {
throw new ClientErrorResponseException($this->parseErrors($clientResponse));
}

$this->logger->debug('client response: ' . json_encode($clientResponse));

$count += $blockSize;
}
@@ -141,7 +149,7 @@ public function deleteDocument($id): bool
$deleteParams = [
'type' => '_doc',
'index' => $document['_index'],
'id' => $document['_id']
'id' => $document['_id'],
];
$this->getClient()->delete($deleteParams);

@@ -154,6 +162,7 @@ public function deleteDocument($id): bool
/**
* @param array $ids
* @param string $type
*
* @return array
*/
public function searchResourceByIds($ids = [])
@@ -214,4 +223,15 @@ private function extendBatch(string $action, string $indexName, IndexDocument $d

return $params;
}

private function parseErrors(array $clientResponse): string
{
$errors = '';

foreach ($clientResponse['items'] as $response) {
$response = reset($response);
bartlomiejmarszal marked this conversation as resolved.
Show resolved Hide resolved
$errors .= $response['error']['reason'];
}
return $errors;
}
}