diff --git a/composer.json b/composer.json index ba4cbc2..ab1eb03 100644 --- a/composer.json +++ b/composer.json @@ -6,6 +6,7 @@ "sort-packages": true }, "require": { + "pimcore/pimcore": "^11.x-dev", "elasticsearch/elasticsearch": "^8.0", "symfony/config": "^5.2.0 || ^6.0", "symfony/dependency-injection": "^5.2.0 || ^6.0", diff --git a/src/DependencyInjection/PimcoreElasticsearchClientExtension.php b/src/DependencyInjection/PimcoreElasticsearchClientExtension.php index 9dd996c..3c1ace6 100644 --- a/src/DependencyInjection/PimcoreElasticsearchClientExtension.php +++ b/src/DependencyInjection/PimcoreElasticsearchClientExtension.php @@ -17,6 +17,7 @@ use Elastic\Elasticsearch\Client; use Pimcore\Bundle\ElasticsearchClientBundle\EsClientFactory; +use Pimcore\Bundle\ElasticsearchClientBundle\SearchClient\SearchClient; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; @@ -28,6 +29,7 @@ class PimcoreElasticsearchClientExtension extends ConfigurableExtension implements PrependExtensionInterface { const CLIENT_SERVICE_PREFIX = 'pimcore.elasticsearch_client.'; + const PIMCORE_CLIENT_PREFIX = 'pimcore.elasticsearch.custom_client.'; protected function loadInternal(array $mergedConfig, ContainerBuilder $container) { @@ -43,6 +45,10 @@ protected function loadInternal(array $mergedConfig, ContainerBuilder $container $definition->setArgument('$configuration', $clientConfig); $definition->addTag('monolog.logger', ['channel' => $clientConfig['logger_channel']]); $definitions[self::CLIENT_SERVICE_PREFIX . $name] = $definition; + + $customClientDefinition = new Definition(SearchClient::class); + $customClientDefinition->setArgument('$client', $definition); + $definitions[self::PIMCORE_CLIENT_PREFIX . $name] = $customClientDefinition; } $container->addDefinitions($definitions); diff --git a/src/SearchClient/ElasticsearchClientInterface.php b/src/SearchClient/ElasticsearchClientInterface.php new file mode 100644 index 0000000..939a4e0 --- /dev/null +++ b/src/SearchClient/ElasticsearchClientInterface.php @@ -0,0 +1,25 @@ +client; + } + + /** + * @throws ClientException + */ + 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()) + ); + } + } + + /** + * @throws ClientException + */ + 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()) + ); + } + } + + /** + * @throws ClientException + */ + 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()) + ); + } + } + + /** + * @throws ClientException + */ + 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()) + ); + } + } + + /** + * @throws ClientException + */ + 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()) + ); + } + } + + /** + * @throws ClientException + */ + 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()) + ); + } + } + + /** + * @throws ClientException + */ + 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()) + ); + } + } + + /** + * @throws ClientException + */ + 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()) + ); + } + } + + /** + * @throws ClientException + */ + 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()) + ); + } + } + + /** + * @throws ClientException + */ + 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()) + ); + } + } + + /** + * @throws ClientException + */ + 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()) + ); + } + } + + /** + * @throws ClientException + */ + 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()) + ); + } + } + + /** + * @throws ClientException + */ + 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()) + ); + } + } + + /** + * @throws ClientException + */ + 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()) + ); + } + } + + /** + * @throws ClientException + */ + 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()) + ); + } + } + + /** + * @throws ClientException + */ + 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()) + ); + } + } + + /** + * @throws ClientException + */ + 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()) + ); + } + } + + /** + * @throws ClientException + */ + 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()) + ); + } + } + + /** + * @throws ClientException + */ + 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()) + ); + } + } + + /** + * @throws ClientException + */ + 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()) + ); + } + } + + /** + * @throws ClientException + */ + 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()) + ); + } + } + + /** + * @throws ClientException + */ + 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()) + ); + } + } + + private function getArrayResponse(Elasticsearch $response): array + { + return $response->asArray(); + } + + private function getBoolResponse(Elasticsearch $response): bool + { + return $response->asBool(); + } +}