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
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion src/ElasticSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function query($queryString, $type, $start = 0, $count = 10, $order = '_i
/**
* (Re)Generate the index for a given resource
* @param IndexIterator|array $documents
* @return integer
* @throws ClientErrorResponseException
*/
public function index($documents = []): int
{
Expand Down
27 changes: 24 additions & 3 deletions src/ElasticSearchIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
namespace oat\tao\elasticsearch;

use Elasticsearch\Client;
use Elasticsearch\Common\Exceptions\ClientErrorResponseException;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Iterator;
Expand Down Expand Up @@ -74,6 +75,7 @@ public function getIndexNameByDocument(IndexDocument $document): string
/**
* @param Iterator $documents
* @return int The number of indexed documents
* @throws ClientErrorResponseException
*/
public function buildIndex(Iterator $documents): int
{
Expand Down Expand Up @@ -101,7 +103,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();

Expand All @@ -110,6 +111,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;
Expand All @@ -121,7 +126,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;
}
Expand All @@ -141,7 +150,7 @@ public function deleteDocument($id): bool
$deleteParams = [
'type' => '_doc',
'index' => $document['_index'],
'id' => $document['_id']
'id' => $document['_id'],
];
$this->getClient()->delete($deleteParams);

Expand All @@ -154,6 +163,7 @@ public function deleteDocument($id): bool
/**
* @param array $ids
* @param string $type
*
* @return array
*/
public function searchResourceByIds($ids = [])
Expand Down Expand Up @@ -214,4 +224,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;
}
}
44 changes: 44 additions & 0 deletions test/ElasticSearchIndexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
namespace oat\tao\test\elasticsearch;

use Elasticsearch\Client;
use Elasticsearch\Common\Exceptions\ClientErrorResponseException;
use oat\generis\test\TestCase;
use oat\oatbox\log\LoggerService;
use oat\tao\elasticsearch\ElasticSearchIndexer;
Expand Down Expand Up @@ -88,6 +89,49 @@ public function testGetIndexNameByDocumentForUnclassifieds(): void
$this->assertSame(IndexerInterface::UNCLASSIFIEDS_DOCUMENTS_INDEX, $indexName);
}

public function testBuildIndexBulkErrorResponse(): void
gabrielfs7 marked this conversation as resolved.
Show resolved Hide resolved
{
$this->expectException(ClientErrorResponseException::class);
$this->expectExceptionMessage('some reasonsome other reason');
gabrielfs7 marked this conversation as resolved.
Show resolved Hide resolved
$document = $this->createMock(IndexDocument::class);
$document->expects($this->any())
->method('getBody')
->willReturn([
'type' => [
TaoOntology::CLASS_URI_ITEM,
],
]);

$document->expects($this->any())
->method('getId')
->willReturn('some_id');

$this->client
->method('bulk')
->willReturn([
'errors' => true,
'items' => [
[
[
'error' => [
'reason' => 'some reason',
],
],
],
[
[
'error' => [
'reason' => 'some other reason',
],
],
],
],
]);
$iterator = $this->createIterator([$document]);

$this->sut->buildIndex($iterator);
}

public function testBuildIndex(): void
{
$document = $this->createMock(IndexDocument::class);
Expand Down