diff --git a/src/SearchClient/SearchClient.php b/src/SearchClient/SearchClient.php index f69e1db..15b4f43 100644 --- a/src/SearchClient/SearchClient.php +++ b/src/SearchClient/SearchClient.php @@ -17,6 +17,7 @@ namespace Pimcore\Bundle\ElasticsearchClientBundle\SearchClient; use Elastic\Elasticsearch\Client; +use Elastic\Elasticsearch\Exception\ClientResponseException; use Elastic\Elasticsearch\Response\Elasticsearch; use Exception; use Pimcore\SearchClient\Exception\ClientException; @@ -44,9 +45,7 @@ public function create(array $params): array try { return $this->getArrayResponse($this->client->create($params)); } catch (Exception $exception) { - throw new ClientException( - sprintf('Failed to create data: %s', $exception->getMessage()) - ); + return $this->handleExceptionsWithArray($exception, 'Failed to create data'); } } @@ -58,9 +57,7 @@ public function search(array $params): array try { return $this->getArrayResponse($this->client->search($params)); } catch (Exception $exception) { - throw new ClientException( - sprintf('Failed to search data: %s', $exception->getMessage()) - ); + return $this->handleExceptionsWithArray($exception, 'Failed to search data'); } } @@ -72,9 +69,7 @@ public function get(array $params): array try { return $this->getArrayResponse($this->client->get($params)); } catch (Exception $exception) { - throw new ClientException( - sprintf('Failed to get data: %s', $exception->getMessage()) - ); + return $this->handleExceptionsWithArray($exception, 'Failed to get data'); } } @@ -86,9 +81,7 @@ public function exists(array $params): bool try { return $this->getBoolResponse($this->client->exists($params)); } catch (Exception $exception) { - throw new ClientException( - sprintf('Failed to check if data exists: %s', $exception->getMessage()) - ); + return $this->handleExceptionsWithBool($exception, 'Failed to check if data exists'); } } @@ -100,9 +93,7 @@ public function count(array $params): array try { return $this->getArrayResponse($this->client->count($params)); } catch (Exception $exception) { - throw new ClientException( - sprintf('Failed to count data: %s', $exception->getMessage()) - ); + return $this->handleExceptionsWithArray($exception, 'Failed to count data'); } } @@ -114,9 +105,7 @@ public function index(array $params): array try { return $this->getArrayResponse($this->client->index($params)); } catch (Exception $exception) { - throw new ClientException( - sprintf('Index operation failed: %s', $exception->getMessage()) - ); + return $this->handleExceptionsWithArray($exception, 'Index operation failed'); } } @@ -128,9 +117,7 @@ public function bulk(array $params): array try { return $this->getArrayResponse($this->client->bulk($params)); } catch (Exception $exception) { - throw new ClientException( - sprintf('Bulk operation failed: %s', $exception->getMessage()) - ); + return $this->handleExceptionsWithArray($exception, 'Bulk operation failed'); } } @@ -142,9 +129,7 @@ public function delete(array $params): array try { return $this->getArrayResponse($this->client->delete($params)); } catch (Exception $exception) { - throw new ClientException( - sprintf('Delete operation failed: %s', $exception->getMessage()) - ); + return $this->handleExceptionsWithArray($exception, 'Delete operation failed'); } } @@ -156,9 +141,7 @@ public function updateByQuery(array $params): array try { return $this->getArrayResponse($this->client->updateByQuery($params)); } catch (Exception $exception) { - throw new ClientException( - sprintf('Failed to update by query: %s', $exception->getMessage()) - ); + return $this->handleExceptionsWithArray($exception, 'Failed to update by query'); } } @@ -170,9 +153,7 @@ public function deleteByQuery(array $params): array try { return $this->getArrayResponse($this->client->updateByQuery($params)); } catch (Exception $exception) { - throw new ClientException( - sprintf('Failed to delete by query: %s', $exception->getMessage()) - ); + return $this->handleExceptionsWithArray($exception, 'Failed to delete by query'); } } @@ -184,9 +165,7 @@ public function createIndex(array $params): array try { return $this->getArrayResponse($this->client->indices()->create($params)); } catch (Exception $exception) { - throw new ClientException( - sprintf('Failed to create index: %s', $exception->getMessage()) - ); + return $this->handleExceptionsWithArray($exception, 'Failed to create index'); } } @@ -198,9 +177,7 @@ public function openIndex(array $params): array try { return $this->getArrayResponse($this->client->indices()->create($params)); } catch (Exception $exception) { - throw new ClientException( - sprintf('Failed to open index: %s', $exception->getMessage()) - ); + return $this->handleExceptionsWithArray($exception, 'Failed to open index'); } } @@ -212,9 +189,7 @@ public function closeIndex(array $params): array try { return $this->getArrayResponse($this->client->indices()->create($params)); } catch (Exception $exception) { - throw new ClientException( - sprintf('Failed to close index: %s', $exception->getMessage()) - ); + return $this->handleExceptionsWithArray($exception, 'Failed to close index'); } } @@ -226,9 +201,7 @@ public function getAllIndices(array $params): array try { return $this->getArrayResponse($this->client->cat()->indices($params)); } catch (Exception $exception) { - throw new ClientException( - sprintf('Failed to get all indices: %s', $exception->getMessage()) - ); + return $this->handleExceptionsWithArray($exception, 'Failed to get all indices'); } } @@ -240,9 +213,7 @@ public function existsIndex(array $params): bool try { return $this->getBoolResponse($this->client->indices()->exists($params)); } catch (Exception $exception) { - throw new ClientException( - sprintf('Failed to check if index exists: %s', $exception->getMessage()) - ); + return $this->handleExceptionsWithBool($exception, 'Failed to check if index exists'); } } @@ -254,9 +225,7 @@ public function reIndex(array $params): array try { return $this->getArrayResponse($this->client->reindex($params)); } catch (Exception $exception) { - throw new ClientException( - sprintf('Failed to reindex: %s', $exception->getMessage()) - ); + return $this->handleExceptionsWithArray($exception, 'Failed to reindex'); } } @@ -268,9 +237,7 @@ public function refreshIndex(array $params = []): array try { return $this->getArrayResponse($this->client->indices()->refresh($params)); } catch (Exception $exception) { - throw new ClientException( - sprintf('Failed to refresh index: %s', $exception->getMessage()) - ); + return $this->handleExceptionsWithArray($exception, 'Failed to refresh index'); } } @@ -282,9 +249,7 @@ public function flushIndex(array $params = []): array try { return $this->getArrayResponse($this->client->indices()->flush($params)); } catch (Exception $exception) { - throw new ClientException( - sprintf('Failed to flush index: %s', $exception->getMessage()) - ); + return $this->handleExceptionsWithArray($exception, 'Failed to flush index'); } } @@ -296,9 +261,7 @@ public function deleteIndex(array $params): array try { return $this->getArrayResponse($this->client->indices()->delete($params)); } catch (Exception $exception) { - throw new ClientException( - sprintf('Failed to delete an index: %s', $exception->getMessage()) - ); + return $this->handleExceptionsWithArray($exception, 'Failed to delete index'); } } @@ -310,9 +273,7 @@ public function existsIndexAlias(array $params): bool try { return $this->getBoolResponse($this->client->indices()->existsAlias($params)); } catch (Exception $exception) { - throw new ClientException( - sprintf('Failed to check if Alias exists: %s', $exception->getMessage()) - ); + return $this->handleExceptionsWithBool($exception, 'Failed to check if Alias exists'); } } @@ -324,9 +285,7 @@ public function getIndexAlias(array $params): array try { return $this->getArrayResponse($this->client->indices()->getAlias($params)); } catch (Exception $exception) { - throw new ClientException( - sprintf('Failed to get an Alias: %s', $exception->getMessage()) - ); + return $this->handleExceptionsWithArray($exception, 'Failed to get an Alias'); } } @@ -338,9 +297,7 @@ public function deleteIndexAlias(array $params): array try { return $this->getArrayResponse($this->client->indices()->deleteAlias($params)); } catch (Exception $exception) { - throw new ClientException( - sprintf('Failed to delete an Alias: %s', $exception->getMessage()) - ); + return $this->handleExceptionsWithArray($exception, 'Failed to delete an Alias'); } } @@ -352,9 +309,7 @@ public function getAllIndexAliases(array $params): array try { return $this->getArrayResponse($this->client->cat()->aliases($params)); } catch (Exception $exception) { - throw new ClientException( - sprintf('Failed to get all index Aliases: %s', $exception->getMessage()) - ); + return $this->handleExceptionsWithArray($exception, 'Failed to get all index Aliases'); } } @@ -366,9 +321,7 @@ public function updateIndexAliases(array $params): array try { return $this->getArrayResponse($this->client->indices()->updateAliases($params)); } catch (Exception $exception) { - throw new ClientException( - sprintf('Failed to update Aliases: %s', $exception->getMessage()) - ); + return $this->handleExceptionsWithArray($exception, 'Failed to update Aliases'); } } @@ -380,9 +333,7 @@ public function putIndexMapping(array $params): array try { return $this->getArrayResponse($this->client->indices()->putMapping($params)); } catch (Exception $exception) { - throw new ClientException( - sprintf('Failed to put Mapping: %s', $exception->getMessage()) - ); + return $this->handleExceptionsWithArray($exception, 'Failed to put mapping'); } } @@ -394,9 +345,7 @@ public function getIndexMapping(array $params): array try { return $this->getArrayResponse($this->client->indices()->getMapping($params)); } catch (Exception $exception) { - throw new ClientException( - sprintf('Failed to get Mapping: %s', $exception->getMessage()) - ); + return $this->handleExceptionsWithArray($exception, 'Failed to get mapping'); } } @@ -408,9 +357,7 @@ public function getIndexSettings(array $params): array try { return $this->getArrayResponse($this->client->indices()->getSettings($params)); } catch (Exception $exception) { - throw new ClientException( - sprintf('Failed to get index settings: %s', $exception->getMessage()) - ); + return $this->handleExceptionsWithArray($exception, 'Failed to get index settings'); } } @@ -422,9 +369,7 @@ public function putIndexSettings(array $params): array try { return $this->getArrayResponse($this->client->indices()->putSettings($params)); } catch (Exception $exception) { - throw new ClientException( - sprintf('Failed to update index settings: %s', $exception->getMessage()) - ); + return $this->handleExceptionsWithArray($exception, 'Failed to update index settings'); } } @@ -436,12 +381,38 @@ public function getIndexStats(array $params): array try { return $this->getArrayResponse($this->client->indices()->stats($params)); } catch (Exception $exception) { - throw new ClientException( - sprintf('Failed to get index stats: %s', $exception->getMessage()) - ); + return $this->handleExceptionsWithArray($exception, 'Failed to get index stats'); } } + /** + * @throws ClientException + */ + private function handleExceptionsWIthBool(Exception $exception, string $message): bool + { + if ($exception instanceof ClientResponseException && $exception->getCode() === 404) { + return false; + } + + throw new ClientException( + sprintf('%s :%s', $message, $exception->getMessage()) + ); + } + + /** + * @throws ClientException + */ + private function handleExceptionsWithArray(Exception $exception, string $message): array + { + if ($exception instanceof ClientResponseException && $exception->getCode() === 404) { + return []; + } + + throw new ClientException( + sprintf('%s :%s', $message, $exception->getMessage()) + ); + } + private function getArrayResponse(Elasticsearch $response): array { return $response->asArray();