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

Introduce IndexNotFoundException (BC Break) #470

Closed
Show file tree
Hide file tree
Changes from all 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: 2 additions & 0 deletions .examples/laravel/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ services:
discovery.type: single-node
xpack.security.enabled: 'false'
cluster.routing.allocation.disk.threshold_enabled: 'false'
action.auto_create_index: 'false'
ports:
- "9200:9200"
healthcheck:
Expand Down Expand Up @@ -34,6 +35,7 @@ services:
environment:
discovery.type: single-node
cluster.routing.allocation.disk.threshold_enabled: 'false'
action.auto_create_index: 'false'
DISABLE_SECURITY_PLUGIN: true
http.port: 9201
ports:
Expand Down
2 changes: 2 additions & 0 deletions .examples/mezzio/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ services:
discovery.type: single-node
xpack.security.enabled: 'false'
cluster.routing.allocation.disk.threshold_enabled: 'false'
action.auto_create_index: 'false'
ports:
- "9200:9200"
healthcheck:
Expand Down Expand Up @@ -34,6 +35,7 @@ services:
environment:
discovery.type: single-node
cluster.routing.allocation.disk.threshold_enabled: 'false'
action.auto_create_index: 'false'
DISABLE_SECURITY_PLUGIN: true
http.port: 9201
ports:
Expand Down
2 changes: 2 additions & 0 deletions .examples/spiral/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ services:
discovery.type: single-node
xpack.security.enabled: 'false'
cluster.routing.allocation.disk.threshold_enabled: 'false'
action.auto_create_index: 'false'
ports:
- "9200:9200"
healthcheck:
Expand Down Expand Up @@ -34,6 +35,7 @@ services:
environment:
discovery.type: single-node
cluster.routing.allocation.disk.threshold_enabled: 'false'
action.auto_create_index: 'false'
DISABLE_SECURITY_PLUGIN: true
http.port: 9201
ports:
Expand Down
2 changes: 2 additions & 0 deletions .examples/symfony/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ services:
discovery.type: single-node
xpack.security.enabled: 'false'
cluster.routing.allocation.disk.threshold_enabled: 'false'
action.auto_create_index: 'false'
ports:
- "9200:9200"
healthcheck:
Expand Down Expand Up @@ -34,6 +35,7 @@ services:
environment:
discovery.type: single-node
cluster.routing.allocation.disk.threshold_enabled: 'false'
action.auto_create_index: 'false'
DISABLE_SECURITY_PLUGIN: true
http.port: 9201
ports:
Expand Down
2 changes: 2 additions & 0 deletions .examples/yii/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ services:
discovery.type: single-node
xpack.security.enabled: 'false'
cluster.routing.allocation.disk.threshold_enabled: 'false'
action.auto_create_index: 'false'
ports:
- "9200:9200"
healthcheck:
Expand Down Expand Up @@ -34,6 +35,7 @@ services:
environment:
discovery.type: single-node
cluster.routing.allocation.disk.threshold_enabled: 'false'
action.auto_create_index: 'false'
DISABLE_SECURITY_PLUGIN: true
http.port: 9201
ports:
Expand Down
2 changes: 2 additions & 0 deletions docs/getting-started/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1511,6 +1511,7 @@ search engine.
discovery.type: single-node
xpack.security.enabled: 'false'
cluster.routing.allocation.disk.threshold_enabled: 'false'
action.auto_create_index: 'false'
ports:
- "9200:9200"
healthcheck:
Expand Down Expand Up @@ -1547,6 +1548,7 @@ search engine.
environment:
discovery.type: single-node
cluster.routing.allocation.disk.threshold_enabled: 'false'
action.auto_create_index: 'false'
DISABLE_SECURITY_PLUGIN: true
ports:
- "9200:9200"
Expand Down
1 change: 1 addition & 0 deletions packages/seal-elasticsearch-adapter/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ services:
discovery.type: single-node
xpack.security.enabled: 'false'
cluster.routing.allocation.disk.threshold_enabled: 'false'
action.auto_create_index: 'false'
ports:
- "9200:9200"
healthcheck:
Expand Down
89 changes: 77 additions & 12 deletions packages/seal-elasticsearch-adapter/src/ElasticsearchIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use CmsIg\Seal\Adapter\BulkHelper;
use CmsIg\Seal\Adapter\IndexerInterface;
use CmsIg\Seal\Marshaller\Marshaller;
use CmsIg\Seal\Schema\Exception\IndexNotFoundException;
use CmsIg\Seal\Schema\Index;
use CmsIg\Seal\Task\SyncTask;
use CmsIg\Seal\Task\TaskInterface;
Expand Down Expand Up @@ -47,14 +48,22 @@ public function save(Index $index, array $document, array $options = []): TaskIn

$document = $this->marshaller->marshall($index->fields, $document);

/** @var Elasticsearch $response */
$response = $this->client->index([
'index' => $index->name,
'id' => (string) $identifier,
'body' => $document,
// TODO refresh should be refactored with async tasks
'refresh' => $options['return_slow_promise_result'] ?? false, // update document immediately, so it is available in the `/_search` api directly
]);
try {
/** @var Elasticsearch $response */
$response = $this->client->index([
'index' => $index->name,
'id' => (string) $identifier,
'body' => $document,
// TODO refresh should be refactored with async tasks
'refresh' => $options['return_slow_promise_result'] ?? false, // update document immediately, so it is available in the `/_search` api directly
]);
} catch (ClientResponseException $e) {
if (404 !== $e->getResponse()->getStatusCode()) {
throw $e;
}

throw new IndexNotFoundException($index->name, $e);
}

if (200 !== $response->getStatusCode() && 201 !== $response->getStatusCode()) {
throw new \RuntimeException('Unexpected error while indexing document with identifier "' . $identifier . '".');
Expand Down Expand Up @@ -85,6 +94,8 @@ public function delete(Index $index, string $identifier, array $options = []): T
if (404 !== $e->getResponse()->getStatusCode()) {
throw $e;
}

throw new IndexNotFoundException($index->name, $e);
}

if (!($options['return_slow_promise_result'] ?? false)) {
Expand Down Expand Up @@ -117,13 +128,40 @@ public function bulk(Index $index, iterable $saveDocuments, iterable $deleteDocu
$params['body'][] = $document;
}

/** @var Elasticsearch $response */
$response = $this->client->bulk($params);
try {
/** @var Elasticsearch $response */
$response = $this->client->bulk($params);
} catch (ClientResponseException $e) {
$response = $e->getResponse();

if (404 === $response->getStatusCode()
&& \str_contains($response->getBody()->__toString(), '"index_not_found_exception"')
) {
throw new IndexNotFoundException($index->name, $e);
}

throw $e;
}

if (200 !== $response->getStatusCode() && 201 !== $response->getStatusCode()) {
throw new \RuntimeException('Unexpected error while bulk indexing documents for index "' . $index->name . '".');
}

$responseData = $response->asArray();

if ($responseData['errors'] ?? false) {
$errorType = $responseData['items'][0]['index']['error']['type']
?? null;
if ('index_not_found_exception' === $errorType) {
throw new IndexNotFoundException(
$index->name,
new \RuntimeException($response->asString()),
);
}

throw new \RuntimeException('Unexpected error while bulk indexing documents for index "' . $index->name . '".');
}

$batchIndexingResponses[] = $response;
}

Expand All @@ -138,13 +176,40 @@ public function bulk(Index $index, iterable $saveDocuments, iterable $deleteDocu
];
}

/** @var Elasticsearch $response */
$response = $this->client->bulk($params);
try {
/** @var Elasticsearch $response */
$response = $this->client->bulk($params);
} catch (ClientResponseException $e) {
$response = $e->getResponse();

if (404 === $response->getStatusCode()
&& \str_contains($response->getBody()->__toString(), '"index_not_found_exception"')
) {
throw new IndexNotFoundException($index->name, $e);
}

throw $e;
}

if (200 !== $response->getStatusCode() && 201 !== $response->getStatusCode()) {
throw new \RuntimeException('Unexpected error while bulk deleting documents for index "' . $index->name . '".');
}

$responseData = $response->asArray();

if ($responseData['errors'] ?? false) {
$errorType = $responseData['items'][0]['delete']['error']['type']
?? null;
if ('index_not_found_exception' === $errorType) {
throw new IndexNotFoundException(
$index->name,
new \RuntimeException($response->asString()),
);
}

throw new \RuntimeException('Unexpected error while bulk indexing documents for index "' . $index->name . '".');
}

$batchIndexingResponses[] = $response;
}

Expand Down
30 changes: 23 additions & 7 deletions packages/seal-elasticsearch-adapter/src/ElasticsearchSearcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use CmsIg\Seal\Adapter\SearcherInterface;
use CmsIg\Seal\Marshaller\Marshaller;
use CmsIg\Seal\Schema\Exception\IndexNotFoundException;
use CmsIg\Seal\Schema\Field;
use CmsIg\Seal\Schema\Index;
use CmsIg\Seal\Search\Condition;
Expand Down Expand Up @@ -59,8 +60,11 @@ public function search(Search $search): Result
$searchResult = $response->asArray();
} catch (ClientResponseException $e) {
$response = $e->getResponse();
if (404 !== $response->getStatusCode()) {
throw $e;

if (404 === $response->getStatusCode()
&& \str_contains($response->getBody()->__toString(), '"index_not_found_exception"')
) {
throw new IndexNotFoundException($search->index->name, $e);
}

return new Result(
Expand Down Expand Up @@ -99,11 +103,23 @@ public function search(Search $search): Result
$body['size'] = $search->limit;
}

/** @var Elasticsearch $response */
$response = $this->client->search([
'index' => $search->index->name,
'body' => $body,
]);
try {
/** @var Elasticsearch $response */
$response = $this->client->search([
'index' => $search->index->name,
'body' => $body,
]);
} catch (ClientResponseException $e) {
$response = $e->getResponse();

if (404 === $response->getStatusCode()
&& \str_contains($response->getBody()->__toString(), '"index_not_found_exception"')
) {
throw new IndexNotFoundException($search->index->name, $e);
}

throw $e;
}

/**
* @var array{
Expand Down
8 changes: 7 additions & 1 deletion packages/seal-loupe-adapter/src/LoupeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CmsIg\Seal\Adapter\Loupe;

use CmsIg\Seal\Schema\Exception\IndexNotFoundException;
use CmsIg\Seal\Schema\Index;
use Loupe\Loupe\Configuration;
use Loupe\Loupe\Loupe;
Expand Down Expand Up @@ -75,6 +76,8 @@ public function dropIndex(Index $index): void
}

\rmdir($indexDirectory);

unset($this->loupe[$index->name]);
}
}

Expand Down Expand Up @@ -112,7 +115,10 @@ private function createLoupe(Index $index, Configuration|null $configuration = n
$configurationFile = $this->getConfigurationFile($index);

if (!\file_exists($configurationFile)) {
throw new \LogicException('Configuration need to exist before creating Loupe instance.');
throw new IndexNotFoundException(
$index->name,
new \LogicException('Configuration need to exist before creating Loupe instance.'),
);
}

/** @var string $configurationContent */
Expand Down
7 changes: 4 additions & 3 deletions packages/seal-memory-adapter/src/MemoryStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CmsIg\Seal\Adapter\Memory;

use CmsIg\Seal\Schema\Exception\IndexNotFoundException;
use CmsIg\Seal\Schema\Index;

/**
Expand All @@ -36,7 +37,7 @@ final class MemoryStorage
public static function getDocuments(Index $index): array
{
if (!\array_key_exists($index->name, self::$indexes)) {
throw new \RuntimeException('Index "' . $index->name . '" does not exist.');
throw new IndexNotFoundException($index->name);
}

return self::$documents[$index->name];
Expand All @@ -50,7 +51,7 @@ public static function getDocuments(Index $index): array
public static function save(Index $index, array $document): array
{
if (!\array_key_exists($index->name, self::$indexes)) {
throw new \RuntimeException('Index "' . $index->name . '" does not exist.');
throw new IndexNotFoundException($index->name);
}

$identifierField = $index->getIdentifierField();
Expand All @@ -66,7 +67,7 @@ public static function save(Index $index, array $document): array
public static function delete(Index $index, string $identifier): void
{
if (!\array_key_exists($index->name, self::$indexes)) {
throw new \RuntimeException('Index "' . $index->name . '" does not exist.');
throw new IndexNotFoundException($index->name);
}

unset(self::$documents[$index->name][$identifier]);
Expand Down
1 change: 1 addition & 0 deletions packages/seal-opensearch-adapter/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ services:
environment:
discovery.type: single-node
cluster.routing.allocation.disk.threshold_enabled: 'false'
action.auto_create_index: 'false'
DISABLE_SECURITY_PLUGIN: true
ports:
- "9200:9200"
Expand Down
26 changes: 26 additions & 0 deletions packages/seal/src/Schema/Exception/IndexNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

/*
* This file is part of the CMS-IG SEAL project.
*
* (c) Alexander Schranz <alexander@sulu.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace CmsIg\Seal\Schema\Exception;

final class IndexNotFoundException extends \RuntimeException
{
public function __construct(string $indexName, \Throwable|null $previous = null)
{
parent::__construct(
'Index "' . $indexName . '" does not exist.',
0,
$previous,
);
}
}
Loading
Loading