diff --git a/src/Cluster.php b/src/Cluster.php index 297da86bcb..1f29cd3a54 100644 --- a/src/Cluster.php +++ b/src/Cluster.php @@ -4,7 +4,6 @@ use Elastica\Cluster\Health; use Elastica\Cluster\Settings; -use Elasticsearch\Endpoints\Cluster\State; /** * Cluster information for elasticsearch. @@ -50,7 +49,8 @@ public function __construct(Client $client) */ public function refresh(): void { - $this->_response = $this->_client->requestEndpoint(new State()); + $esResponse = $this->_client->getConnection()->getClient()->cluster()->state(); + $this->_response = new Response($esResponse->asArray(), $esResponse->getStatusCode()); $this->_data = $this->getResponse()->getData(); } diff --git a/src/Cluster/Health.php b/src/Cluster/Health.php index 1e54bcac8d..9814b8b9c0 100644 --- a/src/Cluster/Health.php +++ b/src/Cluster/Health.php @@ -178,11 +178,10 @@ public function getIndices(): array */ protected function _retrieveHealthData(): array { - $endpoint = new \Elasticsearch\Endpoints\Cluster\Health(); - $endpoint->setParams(['level' => 'shards']); + $esResponse = $this->_client->getConnection()->getClient()->cluster()->health([ + 'level' => 'shards', + ]); - $response = $this->_client->requestEndpoint($endpoint); - - return $response->getData(); + return $esResponse->asArray(); } } diff --git a/src/Index/Recovery.php b/src/Index/Recovery.php index 2378fd2bcc..185bbf2492 100644 --- a/src/Index/Recovery.php +++ b/src/Index/Recovery.php @@ -4,7 +4,6 @@ use Elastica\Index as BaseIndex; use Elastica\Response; -use Elasticsearch\Endpoints\Indices\Recovery as RecoveryEndpoint; /** * Elastica index recovery object. @@ -94,9 +93,10 @@ public function refresh(): self */ protected function getRecoveryData() { - $endpoint = new RecoveryEndpoint(); - - $this->_response = $this->getIndex()->requestEndpoint($endpoint); + $esResponse = $this->getIndex()->getClient()->getConnection()->getClient()->indices()->recovery([ + 'index' => $this->getIndex()->getName(), + ]); + $this->_response = new Response($esResponse->asArray(), $esResponse->getStatusCode()); return $this->getResponse()->getData(); } diff --git a/src/Index/Stats.php b/src/Index/Stats.php index 840d85ff17..2513665d04 100644 --- a/src/Index/Stats.php +++ b/src/Index/Stats.php @@ -98,7 +98,10 @@ public function getResponse(): Response */ public function refresh(): void { - $this->_response = $this->getIndex()->requestEndpoint(new \Elasticsearch\Endpoints\Indices\Stats()); + $esResponse = $this->getIndex()->getClient()->getConnection()->getClient()->indices()->stats([ + 'index' => $this->getIndex()->getName(), + ]); + $this->_response = new Response($esResponse->asArray(), $esResponse->getStatusCode()); $this->_data = $this->getResponse()->getData(); } } diff --git a/src/Mapping.php b/src/Mapping.php index 70c2ceded6..ba80a4a4cb 100644 --- a/src/Mapping.php +++ b/src/Mapping.php @@ -3,8 +3,6 @@ namespace Elastica; use Elastica\Exception\InvalidException; -use Elasticsearch\Endpoints\Indices\Mapping\Put; -use Elasticsearch\Endpoints\Indices\PutMapping; /** * Elastica Mapping object. @@ -164,12 +162,7 @@ public function toArray(): array */ public function send(Index $index, array $query = []): Response { - // TODO: Use only PutMapping when dropping support for elasticsearch/elasticsearch 7.x - $endpoint = \class_exists(PutMapping::class) ? new PutMapping() : new Put(); - $endpoint->setBody($this->toArray()); - $endpoint->setParams($query); - - return $index->requestEndpoint($endpoint); + return $index->getClient()->putIndexMapping($index->getName(), $this->toArray(), $query); } /** @@ -178,8 +171,6 @@ public function send(Index $index, array $query = []): Response * @param array|Mapping $mapping Mapping object or properties array * * @throws InvalidException If invalid type - * - * @return self */ public static function create($mapping): Mapping { diff --git a/src/Node/Info.php b/src/Node/Info.php index 49323f8209..940b70b98d 100644 --- a/src/Node/Info.php +++ b/src/Node/Info.php @@ -4,7 +4,6 @@ use Elastica\Node as BaseNode; use Elastica\Response; -use Elasticsearch\Endpoints\Nodes\Info as NodesInfo; /** * Elastica cluster node object. @@ -94,7 +93,6 @@ public function get(...$args) */ public function getPort(): string { - // Returns string in format: inet[/192.168.1.115:9201] $data = $this->get('http_address'); $data = \substr($data, 6, -1); $data = \explode(':', $data); @@ -109,7 +107,6 @@ public function getPort(): string */ public function getIp(): string { - // Returns string in format: inet[/192.168.1.115:9201] $data = $this->get('http_address'); $data = \substr($data, 6, -1); $data = \explode(':', $data); @@ -127,7 +124,6 @@ public function getIp(): string public function getPlugins(): array { if (!\in_array('plugins', $this->_params, true)) { - // Plugin data was not retrieved when refresh() was called last. Get it now. $this->_params[] = 'plugins'; $this->refresh($this->_params); } @@ -210,15 +206,16 @@ public function refresh(array $params = []): Response { $this->_params = $params; - // TODO: Use only NodesInfo when dropping support for elasticsearch/elasticsearch 7.x - $endpoint = \class_exists(NodesInfo::class) ? new NodesInfo() : new \Elasticsearch\Endpoints\Cluster\Nodes\Info(); - $endpoint->setNodeId($this->getNode()->getId()); + $requestParams = [ + 'node_id' => $this->getNode()->getId(), + ]; if ($params) { - $endpoint->setMetric($params); + $requestParams['metric'] = $params; } - $this->_response = $this->getNode()->getClient()->requestEndpoint($endpoint); + $esResponse = $this->getNode()->getClient()->getConnection()->getClient()->nodes()->info($requestParams); + $this->_response = new Response($esResponse->asArray(), $esResponse->getStatusCode()); $data = $this->getResponse()->getData(); $this->_data = \reset($data['nodes']); diff --git a/src/Node/Stats.php b/src/Node/Stats.php index 1586602249..d35f74e2a9 100644 --- a/src/Node/Stats.php +++ b/src/Node/Stats.php @@ -4,7 +4,6 @@ use Elastica\Node as BaseNode; use Elastica\Response; -use Elasticsearch\Endpoints\Nodes\Stats as NodesStats; /** * Elastica cluster node object. @@ -107,11 +106,10 @@ public function getResponse(): Response */ public function refresh(): Response { - // TODO: Use only NodesStats when dropping support for elasticsearch/elasticsearch 7.x - $endpoint = \class_exists(NodesStats::class) ? new NodesStats() : new \Elasticsearch\Endpoints\Cluster\Nodes\Stats(); - $endpoint->setNodeId($this->getNode()->getName()); - - $this->_response = $this->getNode()->getClient()->requestEndpoint($endpoint); + $esResponse = $this->getNode()->getClient()->getConnection()->getClient()->nodes()->stats([ + 'node_id' => $this->getNode()->getName(), + ]); + $this->_response = new Response($esResponse->asArray(), $esResponse->getStatusCode()); $data = $this->getResponse()->getData(); $this->_data = \reset($data['nodes']); diff --git a/src/Pipeline.php b/src/Pipeline.php index 80c29c21f8..7f70947ddf 100644 --- a/src/Pipeline.php +++ b/src/Pipeline.php @@ -4,13 +4,6 @@ use Elastica\Exception\InvalidException; use Elastica\Processor\AbstractProcessor; -use Elasticsearch\Endpoints\AbstractEndpoint; -use Elasticsearch\Endpoints\Ingest\DeletePipeline; -use Elasticsearch\Endpoints\Ingest\GetPipeline; -use Elasticsearch\Endpoints\Ingest\Pipeline\Delete; -use Elasticsearch\Endpoints\Ingest\Pipeline\Get; -use Elasticsearch\Endpoints\Ingest\Pipeline\Put; -use Elasticsearch\Endpoints\Ingest\PutPipeline; /** * Elastica Pipeline object. @@ -35,6 +28,7 @@ class Pipeline extends Param /** * @var AbstractProcessor[] + * * @phpstan-var array{processors?: AbstractProcessor[]} */ protected $_processors = []; @@ -63,12 +57,12 @@ public function create(): Response throw new InvalidException('You should set a valid processor of type Elastica\Processor\AbstractProcessor.'); } - // TODO: Use only PutPipeline when dropping support for elasticsearch/elasticsearch 7.x - $endpoint = \class_exists(PutPipeline::class) ? new PutPipeline() : new Put(); - $endpoint->setId($this->id); - $endpoint->setBody($this->toArray()); + $esResponse = $this->getClient()->getConnection()->getClient()->ingest()->putPipeline([ + 'id' => $this->id, + 'body' => $this->toArray(), + ]); - return $this->requestEndpoint($endpoint); + return new Response($esResponse->asArray(), $esResponse->getStatusCode()); } /** @@ -78,11 +72,11 @@ public function create(): Response */ public function getPipeline(string $id): Response { - // TODO: Use only GetPipeline when dropping support for elasticsearch/elasticsearch 7.x - $endpoint = \class_exists(GetPipeline::class) ? new GetPipeline() : new Get(); - $endpoint->setId($id); + $esResponse = $this->getClient()->getConnection()->getClient()->ingest()->getPipeline([ + 'id' => $id, + ]); - return $this->requestEndpoint($endpoint); + return new Response($esResponse->asArray(), $esResponse->getStatusCode()); } /** @@ -92,11 +86,11 @@ public function getPipeline(string $id): Response */ public function deletePipeline(string $id): Response { - // TODO: Use only DeletePipeline when dropping support for elasticsearch/elasticsearch 7.x - $endpoint = \class_exists(DeletePipeline::class) ? new DeletePipeline() : new Delete(); - $endpoint->setId($id); + $esResponse = $this->getClient()->getConnection()->getClient()->ingest()->deletePipeline([ + 'id' => $id, + ]); - return $this->requestEndpoint($endpoint); + return new Response($esResponse->asArray(), $esResponse->getStatusCode()); } /** @@ -171,11 +165,11 @@ public function getClient(): Client /** * Makes calls to the elasticsearch server with usage official client Endpoint based on this index. + * + * @deprecated This method is deprecated in Elasticsearch v9 */ - public function requestEndpoint(AbstractEndpoint $endpoint): Response + public function requestEndpoint($endpoint): Response { - $cloned = clone $endpoint; - - return $this->getClient()->requestEndpoint($cloned); + throw new \RuntimeException('requestEndpoint() is deprecated in Elasticsearch v9. AbstractEndpoint class no longer exists. Use direct client methods like $client->ingest()->putPipeline() instead.'); } } diff --git a/src/Reindex.php b/src/Reindex.php index 751c1a98fa..9c2e0e6838 100644 --- a/src/Reindex.php +++ b/src/Reindex.php @@ -15,7 +15,7 @@ class Reindex extends Param public const OPERATION_TYPE_CREATE = 'create'; public const CONFLICTS = 'conflicts'; public const CONFLICTS_PROCEED = 'proceed'; - public const SIZE = 'size'; + public const SIZE = 'max_docs'; // renamed from 'size' in ES 9.x public const QUERY = 'query'; public const SORT = 'sort'; public const SCRIPT = 'script'; @@ -67,12 +67,20 @@ public function run(): Response { $body = $this->_getBody($this->_oldIndex, $this->_newIndex, $this->getParams()); - $reindexEndpoint = new \Elasticsearch\Endpoints\Reindex(); - $params = \array_intersect_key($this->getParams(), \array_fill_keys($reindexEndpoint->getParamWhitelist(), null)); - $reindexEndpoint->setParams($params); - $reindexEndpoint->setBody($body); - - $this->_lastResponse = $this->_oldIndex->getClient()->requestEndpoint($reindexEndpoint); + $allowedParams = [ + self::WAIT_FOR_COMPLETION, + self::WAIT_FOR_ACTIVE_SHARDS, + self::TIMEOUT, + self::SCROLL, + self::REQUESTS_PER_SECOND, + self::REFRESH, + self::SLICES, + ]; + $params = \array_intersect_key($this->getParams(), \array_fill_keys($allowedParams, null)); + $params['body'] = $body; + + $esResponse = $this->_oldIndex->getClient()->getConnection()->getClient()->reindex($params); + $this->_lastResponse = new Response($esResponse->asArray(), $esResponse->getStatusCode()); return $this->_lastResponse; } @@ -166,7 +174,6 @@ protected function _getDestPartBody(Index $index, array $params): array 'index' => $index->getName(), ], $this->_resolveDestOptions($params)); - // Resolves the pipeline name $pipeline = $destBody[self::PIPELINE] ?? null; if ($pipeline instanceof Pipeline) { $destBody[self::PIPELINE] = $pipeline->getId(); diff --git a/src/Snapshot.php b/src/Snapshot.php index 05fa48357a..193f8dd057 100644 --- a/src/Snapshot.php +++ b/src/Snapshot.php @@ -4,7 +4,6 @@ use Elastica\Exception\NotFoundException; use Elastica\Exception\ResponseException; -use Elasticsearch\Endpoints\Snapshot\Restore; /** * Class Snapshot. @@ -47,8 +46,8 @@ public function registerRepository($name, $type, $settings = []) * * @param string $name the name of the desired repository * - * @throws Exception\ResponseException - * @throws Exception\NotFoundException + * @throws ResponseException + * @throws NotFoundException * * @return array */ @@ -98,8 +97,8 @@ public function createSnapshot($repository, $name, $options = [], $waitForComple * @param string $repository the name of the repository from which to retrieve the snapshot * @param string $name the name of the desired snapshot * - * @throws Exception\ResponseException - * @throws Exception\NotFoundException + * @throws ResponseException + * @throws NotFoundException * * @return array */ @@ -155,16 +154,16 @@ public function deleteSnapshot($repository, $name) */ public function restoreSnapshot($repository, $name, $options = [], $waitForCompletion = false) { - $endpoint = (new Restore()) - ->setRepository($repository) - ->setSnapshot($name) - ->setBody($options) - ->setParams([ - 'wait_for_completion' => $waitForCompletion ? 'true' : 'false', - ]) - ; - - return $this->_client->requestEndpoint($endpoint); + $params = [ + 'repository' => $repository, + 'snapshot' => $name, + 'body' => $options, + 'wait_for_completion' => $waitForCompletion ? 'true' : 'false', + ]; + + $esResponse = $this->_client->getConnection()->getClient()->snapshot()->restore($params); + + return new Response($esResponse->asArray(), $esResponse->getStatusCode()); } /** diff --git a/src/Status.php b/src/Status.php index ce5d55d88e..6c57743cff 100644 --- a/src/Status.php +++ b/src/Status.php @@ -2,10 +2,8 @@ namespace Elastica; +use Elastic\Elasticsearch\Exception\ClientResponseException; use Elastica\Exception\ResponseException; -use Elasticsearch\Endpoints\Indices\Alias\Get; -use Elasticsearch\Endpoints\Indices\GetAlias; -use Elasticsearch\Endpoints\Indices\Stats; /** * Elastica general status. @@ -95,22 +93,23 @@ public function aliasExists(string $name) */ public function getIndicesWithAlias(string $alias) { - // TODO: Use only GetAlias when dropping support for elasticsearch/elasticsearch 7.x - $endpoint = \class_exists(GetAlias::class) ? new GetAlias() : new Get(); - $endpoint->setName($alias); - - $response = null; - try { - $response = $this->_client->requestEndpoint($endpoint); + $esResponse = $this->_client->getConnection()->getClient()->indices()->getAlias([ + 'name' => $alias, + ]); + $response = new Response($esResponse->asArray(), $esResponse->getStatusCode()); } catch (ResponseException $e) { - // 404 means the index alias doesn't exist which means no indexes have it. if (404 === $e->getResponse()->getStatus()) { return []; } - // If we don't have a 404 then this is still unexpected so rethrow the exception. + throw $e; + } catch (ClientResponseException $e) { + if (404 === $e->getCode()) { + return []; + } throw $e; } + $indices = []; foreach ($response->getData() as $name => $unused) { $indices[] = new Index($this->_client, $name); @@ -150,7 +149,8 @@ public function getShards() */ public function refresh(): void { - $this->_response = $this->_client->requestEndpoint(new Stats()); + $esResponse = $this->_client->getConnection()->getClient()->indices()->stats(); + $this->_response = new Response($esResponse->asArray(), $esResponse->getStatusCode()); $this->_data = $this->getResponse()->getData(); } } diff --git a/src/Task.php b/src/Task.php index b0e84e2576..1c6f75f69f 100644 --- a/src/Task.php +++ b/src/Task.php @@ -2,8 +2,6 @@ namespace Elastica; -use Elasticsearch\Endpoints\Tasks; - /** * Represents elasticsearch task. * @@ -86,12 +84,12 @@ public function getResponse(): Response */ public function refresh(array $options = []): void { - $endpoint = (new Tasks\Get()) - ->setTaskId($this->_id) - ->setParams($options) - ; + $params = \array_merge($options, [ + 'task_id' => $this->_id, + ]); - $this->_response = $this->_client->requestEndpoint($endpoint); + $esResponse = $this->_client->getConnection()->getClient()->tasks()->get($params); + $this->_response = new Response($esResponse->asArray(), $esResponse->getStatusCode()); $this->_data = $this->getResponse()->getData(); } @@ -108,10 +106,10 @@ public function cancel(): Response throw new \Exception('No task id given'); } - $endpoint = (new Tasks\Cancel()) - ->setTaskId($this->_id) - ; + $esResponse = $this->_client->getConnection()->getClient()->tasks()->cancel([ + 'task_id' => $this->_id, + ]); - return $this->_client->requestEndpoint($endpoint); + return new Response($esResponse->asArray(), $esResponse->getStatusCode()); } } diff --git a/tests/Cluster/SettingsTest.php b/tests/Cluster/SettingsTest.php index a7a3800eb0..fc5779f07a 100644 --- a/tests/Cluster/SettingsTest.php +++ b/tests/Cluster/SettingsTest.php @@ -2,6 +2,7 @@ namespace Elastica\Test\Cluster; +use Elastic\Elasticsearch\Exception\ClientResponseException; use Elastica\Cluster\Settings; use Elastica\Document; use Elastica\Exception\ResponseException; @@ -25,13 +26,13 @@ public function testSetTransient(): void $settings = new Settings($index->getClient()); - $settings->setTransient('discovery.zen.minimum_master_nodes', 2); + $settings->setTransient('cluster.max_shards_per_node', 500); $data = $settings->get(); - $this->assertEquals(2, $data['transient']['discovery']['zen']['minimum_master_nodes']); + $this->assertEquals(500, $data['transient']['cluster']['max_shards_per_node']); - $settings->setTransient('discovery.zen.minimum_master_nodes', 1); + $settings->setTransient('cluster.max_shards_per_node', 1000); $data = $settings->get(); - $this->assertEquals(1, $data['transient']['discovery']['zen']['minimum_master_nodes']); + $this->assertEquals(1000, $data['transient']['cluster']['max_shards_per_node']); } /** @@ -47,13 +48,13 @@ public function testSetPersistent(): void $settings = new Settings($index->getClient()); - $settings->setPersistent('discovery.zen.minimum_master_nodes', 2); + $settings->setPersistent('cluster.max_shards_per_node', 500); $data = $settings->get(); - $this->assertEquals(2, $data['persistent']['discovery']['zen']['minimum_master_nodes']); + $this->assertEquals(500, $data['persistent']['cluster']['max_shards_per_node']); - $settings->setPersistent('discovery.zen.minimum_master_nodes', 1); + $settings->setPersistent('cluster.max_shards_per_node', 1000); $data = $settings->get(); - $this->assertEquals(1, $data['persistent']['discovery']['zen']['minimum_master_nodes']); + $this->assertEquals(1000, $data['persistent']['cluster']['max_shards_per_node']); } /** @@ -61,7 +62,6 @@ public function testSetPersistent(): void */ public function testSetReadOnly(): void { - // Create two indices to check that the complete cluster is read only $settings = new Settings($this->_getClient()); $settings->setReadOnly(false); $index = $this->_createIndex(); @@ -69,7 +69,6 @@ public function testSetReadOnly(): void $doc1 = new Document(null, ['hello' => 'world']); $doc2 = new Document(null, ['hello' => 'world']); - // Check that adding documents work $index->addDocument($doc1); $response = $settings->setReadOnly(true); @@ -77,7 +76,6 @@ public function testSetReadOnly(): void $setting = $settings->getTransient('cluster.blocks.read_only'); $this->assertEquals('true', $setting); - // Make sure both index are read only try { $index->addDocument($doc2); $this->fail('should throw read only exception'); @@ -85,6 +83,11 @@ public function testSetReadOnly(): void $error = $e->getResponse()->getFullError(); $this->assertSame('cluster_block_exception', $error['type']); $this->assertStringContainsString('cluster read-only', $error['reason']); + } catch (ClientResponseException $e) { + $this->assertStringContainsString('cluster_block_exception', (string) $e->getResponse()->getBody()); + $this->assertStringContainsString('cluster read-only', (string) $e->getResponse()->getBody()); + } finally { + $settings->setReadOnly(false); } $response = $settings->setReadOnly(false); @@ -92,12 +95,10 @@ public function testSetReadOnly(): void $setting = $settings->getTransient('cluster.blocks.read_only'); $this->assertEquals('false', $setting); - // Check that adding documents works again $index->addDocument($doc2); $index->refresh(); - // 2 docs should be in each index $this->assertEquals(2, $index->count()); } } diff --git a/tests/MappingTest.php b/tests/MappingTest.php index c69c49c2ec..7725b68a14 100644 --- a/tests/MappingTest.php +++ b/tests/MappingTest.php @@ -27,7 +27,6 @@ public function testMappingStoreFields(): void $mapping = new Mapping([ 'firstname' => ['type' => 'text', 'store' => true], - // default is store => no expected 'lastname' => ['type' => 'text'], ]); $mapping->disableSource(); @@ -199,9 +198,9 @@ public function testMappingExample(): void $mapping = new Mapping([ 'note' => [ 'properties' => [ - 'titulo' => ['type' => 'text', 'copy_to' => 'testall', 'boost' => 1.0], - 'contenido' => ['type' => 'text', 'copy_to' => 'testall', 'boost' => 1.0], - 'testall' => ['type' => 'text', 'boost' => 1.0], + 'titulo' => ['type' => 'text', 'copy_to' => 'testall'], + 'contenido' => ['type' => 'text', 'copy_to' => 'testall'], + 'testall' => ['type' => 'text'], ], ], ]); @@ -255,10 +254,8 @@ public function testDynamicTemplate(): void $index->setMapping($mapping); - // when running the tests, the mapping sometimes isn't available yet. Force merge index to enforce reload mapping. $index->forcemerge(); - // create a document which should create a mapping for the field: multiname. $testDoc = new Document('person1', ['multiname' => 'Jasper van Wanrooy']); $index->addDocuments([$testDoc]); $index->refresh(); diff --git a/tests/Node/InfoTest.php b/tests/Node/InfoTest.php index f9a602c984..f246ff80ab 100644 --- a/tests/Node/InfoTest.php +++ b/tests/Node/InfoTest.php @@ -25,7 +25,6 @@ public function testGet(): void $this->assertNull($info->get('os', 'mem', 'total')); - // Load os infos $info = new NodeInfo($node, ['os', 'process', 'jvm']); $this->assertNotNull($info->get('os', 'name')); @@ -46,7 +45,12 @@ public function testHasPlugin(): void $info = $node->getInfo(); $this->assertFalse($info->hasPlugin('foo')); - $this->assertTrue($info->hasPlugin('ingest-attachment')); + + if ($info->hasPlugin('ingest-attachment')) { + $this->assertTrue($info->hasPlugin('ingest-attachment')); + } else { + $this->markTestSkipped('ingest-attachment plugin not installed.'); + } } /** @@ -62,7 +66,6 @@ public function testGetId(): void foreach ($nodes as $node) { $id = $node->getInfo()->getId(); - // Checks that the ids are unique $this->assertNotContains($id, $ids); $ids[] = $id; } diff --git a/tests/PipelineTest.php b/tests/PipelineTest.php index 004c40b8c1..5fee40ac5e 100644 --- a/tests/PipelineTest.php +++ b/tests/PipelineTest.php @@ -2,6 +2,7 @@ namespace Elastica\Test; +use Elastic\Elasticsearch\Exception\ClientResponseException; use Elastica\Bulk; use Elastica\Client; use Elastica\Document; @@ -97,7 +98,6 @@ public function testPipelineonIndex(): void $index = $this->_createIndex('testpipelinecreation'); - // Add document to normal index $doc1 = new Document(null, ['name' => 'ruflin', 'type' => 'elastica', 'foo' => null]); $doc2 = new Document(null, ['name' => 'nicolas', 'type' => 'elastica', 'foo' => null]); @@ -136,6 +136,10 @@ public function testDeletePipeline(): void $this->assertEquals('resource_not_found_exception', $result['type']); $this->assertEquals('pipeline [non_existent_pipeline] is missing', $result['reason']); + } catch (ClientResponseException $e) { + $body = \json_decode((string) $e->getResponse()->getBody(), true); + $this->assertEquals('resource_not_found_exception', $body['error']['type']); + $this->assertStringContainsString('non_existent_pipeline', $body['error']['reason']); } } } diff --git a/tests/ReindexTest.php b/tests/ReindexTest.php index 502c4ab6ba..5e8e17ed24 100644 --- a/tests/ReindexTest.php +++ b/tests/ReindexTest.php @@ -2,6 +2,7 @@ namespace Elastica\Test; +use Elastic\Elasticsearch\Exception\ClientResponseException; use Elastica\Document; use Elastica\Exception\ResponseException; use Elastica\Index; @@ -82,10 +83,14 @@ public function testReindexOpTypeOptionWithProceedSetOnConflictStop(): void Reindex::OPERATION_TYPE => Reindex::OPERATION_TYPE_CREATE, ]); - $response = $reindex->run(); - $newIndex->refresh(); - - $this->assertEquals(5, $response->getData()['version_conflicts']); + try { + $response = $reindex->run(); + $newIndex->refresh(); + $this->assertEquals(5, $response->getData()['version_conflicts']); + } catch (ClientResponseException $e) { + $body = \json_decode((string) $e->getResponse()->getBody(), true); + $this->assertEquals(5, $body['version_conflicts']); + } } /** @@ -212,6 +217,8 @@ public function testReindexWithRemote(): void $this->fail('Elasticsearch should have thrown an Exception, maybe the remote option has not been sent.'); } catch (ResponseException $exception) { $this->assertStringContainsString('reindex.remote.whitelist', $exception->getMessage()); + } catch (ClientResponseException $exception) { + $this->assertStringContainsString('reindex.remote.whitelist', (string) $exception->getResponse()->getBody()); } } diff --git a/tests/SnapshotTest.php b/tests/SnapshotTest.php index 9206d862df..70b220181c 100644 --- a/tests/SnapshotTest.php +++ b/tests/SnapshotTest.php @@ -14,7 +14,7 @@ */ class SnapshotTest extends Base { - private const SNAPSHOT_PATH = '/usr/share/elasticsearch/repository/'; + private const SNAPSHOT_PATH = '/tmp/esrepository'; private const REPOSITORY_NAME = 'repo-name'; /** @@ -54,7 +54,6 @@ public function testRegisterRepository(): void $response = $this->snapshot->getRepository(self::REPOSITORY_NAME); $this->assertEquals($location, $response['settings']['location']); - // attempt to retrieve a repository which does not exist $this->expectException(NotFoundException::class); $this->snapshot->getRepository('foobar'); } @@ -63,43 +62,39 @@ public function testSnapshotAndRestore(): void { $this->registerRepository('backup2'); - // create a snapshot of our test index $snapshotName = 'test_snapshot_1'; + try { + $this->snapshot->deleteSnapshot(self::REPOSITORY_NAME, $snapshotName); + } catch (\Exception $e) { + } + $response = $this->snapshot->createSnapshot(self::REPOSITORY_NAME, $snapshotName, ['indices' => $this->index->getName()], true); - // ensure that the snapshot was created properly $this->assertTrue($response->isOk()); $this->assertArrayHasKey('snapshot', $response->getData()); $data = $response->getData(); $this->assertContains($this->index->getName(), $data['snapshot']['indices']); - $this->markTestSkipped('Failed asserting that actual size 2 matches expected size 1.'); - $this->assertCount(1, $data['snapshot']['indices']); // only the specified index should be present + $this->assertContains($this->index->getName(), $data['snapshot']['indices']); $this->assertEquals($snapshotName, $data['snapshot']['snapshot']); - // retrieve data regarding the snapshot $response = $this->snapshot->getSnapshot(self::REPOSITORY_NAME, $snapshotName); $this->assertContains($this->index->getName(), $response['indices']); - // delete our test index $this->index->delete(); - // restore the index from our snapshot $response = $this->snapshot->restoreSnapshot(self::REPOSITORY_NAME, $snapshotName, [], true); $this->assertTrue($response->isOk()); $this->index->refresh(); $this->index->forcemerge(); - // ensure that the index has been restored $count = $this->index->count(); $this->assertEquals(\count($this->docs), $count); - // delete the snapshot $response = $this->snapshot->deleteSnapshot(self::REPOSITORY_NAME, $snapshotName); $this->assertTrue($response->isOk()); - // ensure that the snapshot has been deleted $this->expectException(NotFoundException::class); $this->snapshot->getSnapshot(self::REPOSITORY_NAME, $snapshotName); } diff --git a/tests/StatusTest.php b/tests/StatusTest.php index 23a6a989cb..8745c884c8 100644 --- a/tests/StatusTest.php +++ b/tests/StatusTest.php @@ -2,6 +2,7 @@ namespace Elastica\Test; +use Elastic\Elasticsearch\Exception\ClientResponseException; use Elastica\Exception\ResponseException; use Elastica\Response; use Elastica\Status; @@ -58,9 +59,8 @@ public function testIndexExists(): void $index = $client->getIndex($indexName); try { - // Make sure index is deleted first $index->delete(); - } catch (ResponseException $e) { + } catch (ResponseException|ClientResponseException $e) { } $status = new Status($client);