From 8b6cdbce1c5c2436fa2458fc1c9f698d0fc760c7 Mon Sep 17 00:00:00 2001 From: Lukas Jansen <33984099+lukas-jansen@users.noreply.github.com> Date: Sat, 20 May 2023 18:57:42 +0200 Subject: [PATCH] Add refresh search analyzers api endpoint (#154) * Add refresh search analyzers api endpoint Signed-off-by: Lukas Jansen <33984099+lukas-jansen@users.noreply.github.com> Signed-off-by: GitHub * Add unit test for RefreshSearchAnalyzers endpoint Signed-off-by: Lukas Jansen Signed-off-by: GitHub * Add changelog + fix phpstan Signed-off-by: Lukas Jansen <33984099+lukas-jansen@users.noreply.github.com> --------- Signed-off-by: Lukas Jansen <33984099+lukas-jansen@users.noreply.github.com> Signed-off-by: GitHub Signed-off-by: Lukas Jansen --- CHANGELOG.md | 1 + .../Indices/RefreshSearchAnalyzers.php | 48 ++++++++ .../Namespaces/IndicesNamespace.php | 17 +++ .../Endpoints/RefreshSearchAnalyzersTest.php | 109 ++++++++++++++++++ 4 files changed, 175 insertions(+) create mode 100644 src/OpenSearch/Endpoints/Indices/RefreshSearchAnalyzers.php create mode 100644 tests/Endpoints/RefreshSearchAnalyzersTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 32a8e2c1..26273e8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Added class docs generator ([#96](https://github.com/opensearch-project/opensearch-php/pull/96)) - Added support for Amazon OpenSearch Serverless SigV4 signing ([#119](https://github.com/opensearch-project/opensearch-php/pull/119)) - Added `includePortInHostHeader` option to `ClientBuilder::fromConfig` ([#118](https://github.com/opensearch-project/opensearch-php/pull/118)) +- Added the `RefreshSearchAnalyzers` endpoint ([[#152](https://github.com/opensearch-project/opensearch-php/issues/152)) ### Changed diff --git a/src/OpenSearch/Endpoints/Indices/RefreshSearchAnalyzers.php b/src/OpenSearch/Endpoints/Indices/RefreshSearchAnalyzers.php new file mode 100644 index 00000000..8ea86074 --- /dev/null +++ b/src/OpenSearch/Endpoints/Indices/RefreshSearchAnalyzers.php @@ -0,0 +1,48 @@ +index ?? null; + + if (isset($index)) { + return "/_plugins/_refresh_search_analyzers/$index"; + } + throw new RuntimeException('Missing index parameter for the endpoint indices.refresh_search_analyzers'); + } + + public function getParamWhitelist(): array + { + return []; + } + + public function getMethod(): string + { + return 'POST'; + } +} diff --git a/src/OpenSearch/Namespaces/IndicesNamespace.php b/src/OpenSearch/Namespaces/IndicesNamespace.php index 3255f8b5..488b6592 100644 --- a/src/OpenSearch/Namespaces/IndicesNamespace.php +++ b/src/OpenSearch/Namespaces/IndicesNamespace.php @@ -1173,6 +1173,23 @@ public function getDataStream(array $params = []) return $this->performRequest($endpoint); } + /** + * $params['index'] = (list) A comma-separated list of index names to refresh analyzers for + * + * @param array $params Associative array of parameters + * @return array + */ + public function refreshSearchAnalyzers(array $params = []) + { + $index = $this->extractArgument($params, 'index'); + + $endpointBuilder = $this->endpoints; + $endpoint = $endpointBuilder('Indices\RefreshSearchAnalyzers'); + $endpoint->setParams($params); + $endpoint->setIndex($index); + + return $this->performRequest($endpoint); + } /** * $params['index'] = (list) A comma-separated list of index names to reload analyzers for * $params['ignore_unavailable'] = (boolean) Whether specified concrete indices should be ignored when unavailable (missing or closed) diff --git a/tests/Endpoints/RefreshSearchAnalyzersTest.php b/tests/Endpoints/RefreshSearchAnalyzersTest.php new file mode 100644 index 00000000..034d97ef --- /dev/null +++ b/tests/Endpoints/RefreshSearchAnalyzersTest.php @@ -0,0 +1,109 @@ +instance = new RefreshSearchAnalyzers(); + } + + public function testGetURIWhenIndexAndIdAreDefined(): void + { + // Arrange + $expected = '/_plugins/_refresh_search_analyzers/index'; + + $this->instance->setIndex('index'); + $this->instance->setId(10); + + // Act + $result = $this->instance->getURI(); + + // Assert + $this->assertEquals($expected, $result); + } + + public function testGetURIWhenIndexIsDefinedAndIdIsNotDefined(): void + { + // Arrange + $expected = '/_plugins/_refresh_search_analyzers/index'; + + $this->instance->setIndex('index'); + + // Act + $result = $this->instance->getURI(); + + // Assert + $this->assertEquals($expected, $result); + } + + public function testGetURIWhenIndexIsNotDefined(): void + { + // Arrange + $expected = RuntimeException::class; + $expectedMessage = 'Missing index parameter for the endpoint indices.refresh_search_analyzers'; + + // Assert + $this->expectException($expected); + $this->expectExceptionMessage($expectedMessage); + + // Act + $this->instance->getURI(); + } + + public function testGetMethodWhenIdIsDefined(): void + { + // Arrange + $expected = 'POST'; + + $this->instance->setId(10); + + // Act + $result = $this->instance->getMethod(); + + // Assert + $this->assertEquals($expected, $result); + } + + public function testGetMethodWhenIdIsNotDefined(): void + { + // Arrange + $expected = 'POST'; + + // Act + $result = $this->instance->getMethod(); + + // Assert + $this->assertEquals($expected, $result); + } +}