diff --git a/Makefile b/Makefile index 089850e..be61188 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ test: ## Run test suite ./vendor/bin/simple-phpunit start: ## Start testing tools (Elasticsearch) - docker run --rm -d --name "elastically_es" -p 9999:9200 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch-oss:7.8.0 + docker run --rm -d --name "elastically_es" -p 9999:9200 -e "discovery.type=single-node" -e "xpack.security.enabled=false" -e "action.destructive_requires_name=false" -it -m 1GB docker.elastic.co/elasticsearch/elasticsearch:8.14.2 start_opensearch: ## Start testing tools (OpenSearch) docker run --rm -d --name "elastically_es" -p 9999:9200 -e "discovery.type=single-node" -e "plugins.security.disabled=true" opensearchproject/opensearch:2.3.0 diff --git a/composer.json b/composer.json index 9384ccd..2a90a4f 100644 --- a/composer.json +++ b/composer.json @@ -17,11 +17,13 @@ "require": { "php": ">=8.0", "ext-json": "*", + "nyholm/psr7": "^1.8", "phpdocumentor/reflection-docblock": "^4.4|^5.0", - "ruflin/elastica": "^7.0", + "ruflin/elastica": "^8.0", "symfony/deprecation-contracts": "^2.4 || ^3.0", "symfony/property-access": "^5.4 || ^6.0 || ^7.0", "symfony/property-info": "^5.4 || ^6.0 || ^7.0", + "symfony/psr-http-message-bridge": "^7.1", "symfony/serializer": "^5.4 || ^6.0 || ^7.0", "symfony/yaml": "^5.4 || ^6.0 || ^7.0" }, @@ -53,7 +55,8 @@ "config": { "sort-packages": true, "allow-plugins": { - "symfony/flex": true + "symfony/flex": true, + "php-http/discovery": true } } } diff --git a/src/Client.php b/src/Client.php index 088549c..a1dd4f5 100644 --- a/src/Client.php +++ b/src/Client.php @@ -31,9 +31,9 @@ class Client extends ElasticaClient private ResultSetBuilder $resultSetBuilder; private IndexNameMapper $indexNameMapper; - public function __construct($config = [], ?callable $callback = null, ?LoggerInterface $logger = null, ?ResultSetBuilder $resultSetBuilder = null, ?IndexNameMapper $indexNameMapper = null) + public function __construct($config = [], ?LoggerInterface $logger = null, ?ResultSetBuilder $resultSetBuilder = null, ?IndexNameMapper $indexNameMapper = null) { - parent::__construct($config, $callback, $logger); + parent::__construct($config, $logger); // BC Layer, to remove in 2.0 $this->factory = new Factory($config); diff --git a/src/Factory.php b/src/Factory.php index 5468db4..c77dad6 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -65,7 +65,6 @@ public function buildClient(): Client return $this->client ??= new Client( $this->config, null, - null, $this->buildBuilder(), $this->buildIndexNameMapper() ); diff --git a/src/IndexBuilder.php b/src/IndexBuilder.php index 3c5b056..70f3fa5 100644 --- a/src/IndexBuilder.php +++ b/src/IndexBuilder.php @@ -14,10 +14,8 @@ use Elastica\Exception\ExceptionInterface; use Elastica\Exception\RuntimeException; use Elastica\Reindex; -use Elastica\Request; use Elastica\Response; use Elastica\Task; -use Elasticsearch\Endpoints\Cluster\State; use JoliCode\Elastically\Mapping\MappingProviderInterface; class IndexBuilder @@ -64,7 +62,9 @@ public function markAsLive(Index $index, string $indexName): Response $data['actions'][] = ['remove' => ['index' => $indexPrefixedName . '*', 'alias' => $indexPrefixedName]]; $data['actions'][] = ['add' => ['index' => $index->getName(), 'alias' => $indexPrefixedName]]; - return $this->client->request('_aliases', Request::POST, $data); + return $this->client->toElasticaResponse( + $this->client->indices()->updateAliases(['index' => $indexName, 'body' => $data]) + ); } /** @@ -119,13 +119,11 @@ public function purgeOldIndices(string $indexName, bool $dryRun = false): array { $indexName = $this->indexNameMapper->getPrefixedIndex($indexName); - $stateRequest = new State(); - $stateRequest->setParams([ + $state = $this->client->cluster()->state([ 'filter_path' => 'metadata.indices.*.state,metadata.indices.*.aliases', ]); - $indexes = $this->client->requestEndpoint($stateRequest); - $indexes = $indexes->getData(); + $indexes = $this->client->toElasticaResponse($state)->getData(); $indexes = $indexes['metadata']['indices']; foreach ($indexes as $realIndexName => &$data) { diff --git a/src/Indexer.php b/src/Indexer.php index 506779b..504c644 100644 --- a/src/Indexer.php +++ b/src/Indexer.php @@ -156,6 +156,11 @@ public function setBulkRequestParams(array $bulkRequestParams): void $this->refreshBulkRequestParams(); } + public function getClient(): Client + { + return $this->client; + } + protected function getCurrentBulk(): Bulk { if (!$this->currentBulk) { diff --git a/src/Messenger/IndexationRequestHandler.php b/src/Messenger/IndexationRequestHandler.php index 241bd0e..cab76ac 100644 --- a/src/Messenger/IndexationRequestHandler.php +++ b/src/Messenger/IndexationRequestHandler.php @@ -17,7 +17,6 @@ use JoliCode\Elastically\Client; use JoliCode\Elastically\Indexer; use JoliCode\Elastically\IndexNameMapper; -use Psr\Log\NullLogger; use Symfony\Component\Messenger\Attribute\AsMessageHandler; use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException; use Symfony\Component\Messenger\MessageBusInterface; @@ -50,9 +49,6 @@ public function __construct(Client $client, MessageBusInterface $bus, DocumentEx $this->exchanger = $exchanger; $this->indexer = $indexer; $this->indexNameMapper = $indexNameMapper; - - // Disable the logs for memory concerns - $this->client->setLogger(new NullLogger()); } /** diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php index 5e46f29..b6d12c5 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -13,6 +13,8 @@ namespace JoliCode\Elastically\Tests; +use Elastica\Request; +use Http\Discovery\Psr17Factory; use JoliCode\Elastically\Client; use JoliCode\Elastically\Factory; use PHPUnit\Framework\TestCase; @@ -21,7 +23,7 @@ abstract class BaseTestCase extends TestCase { protected function setUp(): void { - $this->getFactory()->buildClient()->request('*', 'DELETE'); + $this->getClient()->sendRequest((new Psr17Factory())->createRequest(Request::DELETE, '*')); } protected function getFactory(?string $path = null, array $config = []): Factory @@ -29,7 +31,7 @@ protected function getFactory(?string $path = null, array $config = []): Factory return new Factory($config + [ Factory::CONFIG_MAPPINGS_DIRECTORY => $path ?? __DIR__ . '/configs', 'log' => false, - 'port' => '9999', + 'hosts' => ['http://127.0.0.1:9999'], ]); } diff --git a/tests/IndexBuilderTest.php b/tests/IndexBuilderTest.php index f17f612..76b7a82 100644 --- a/tests/IndexBuilderTest.php +++ b/tests/IndexBuilderTest.php @@ -13,9 +13,9 @@ namespace JoliCode\Elastically\Tests; +use Elastic\Elasticsearch\Exception\ClientResponseException; use Elastica\Document as ElasticaDocument; use Elastica\Exception\InvalidException; -use Elastica\Exception\ResponseException; use Elastica\Index; use Elastica\Index\Settings; use JoliCode\Elastically\Factory; @@ -175,7 +175,7 @@ public function testPurgeAndCloseOldIndices(IndexBuilder $indexBuilder): void try { $index2->search(); $this->assertFalse(true, 'Search should throw a "closed index" exception.'); - } catch (ResponseException $e) { + } catch (ClientResponseException $e) { $this->assertStringContainsStringIgnoringCase('closed', $e->getMessage()); } } diff --git a/tests/IndexerTest.php b/tests/IndexerTest.php index 8147fbb..ee96917 100644 --- a/tests/IndexerTest.php +++ b/tests/IndexerTest.php @@ -160,8 +160,8 @@ public function testRequestParameters(): void $response = $indexer->flush(); $this->assertInstanceOf(ResponseSet::class, $response); - $transferInfo = $response->getTransferInfo(); - $this->assertStringContainsString('_bulk?refresh=wait_for', $transferInfo['url']); + $query = $indexer->getClient()->getLastRequest()->getUri()->getQuery(); + $this->assertStringContainsString('refresh=wait_for', $query); // Test the same with an invalid pipeline $indexer->setBulkRequestParams([ diff --git a/tests/Jane/JaneTest.php b/tests/Jane/JaneTest.php index 35e4373..39b5343 100644 --- a/tests/Jane/JaneTest.php +++ b/tests/Jane/JaneTest.php @@ -66,8 +66,7 @@ public function testCreateIndexAndSearchWithJaneObject() // Build Elastically Client $elastically = new Client( - ['port' => '9999'], - null, + ['hosts' => ['http://127.0.0.1:9999']], null, $resultSetBuilder, $indexNameMapper