diff --git a/CHANGELOG.md b/CHANGELOG.md index eb22ee4a8..7378bb18d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +## Release 6.7.1 + +- Fix #846 choosing `GET` and `POST` in endpoints based on body [[acbc76d0]](https://github.com/elastic/elasticsearch-php/commit/acbc76d0) +- Fix #843 adding `wait_for_active_shards` and `pipeline` in `UpdateByQuery` [[acbc76d0]](https://github.com/elastic/elasticsearch-php/commit/acbc76d0) +- Fixed missing `ScriptsPainlessExecute` endpoint, since ES 6.3 [[acbc76d0]](https://github.com/elastic/elasticsearch-php/commit/acbc76d0) +- Fixed missing `RankEval` endpoint, since ES 6.2 [[acbc76d0]](https://github.com/elastic/elasticsearch-php/commit/acbc76d0) +- Added User-Agent header equal to `elasticsearch-php/6.7.1 (metadata-values)` [[acbc76d0]](https://github.com/elastic/elasticsearch-php/commit/acbc76d0) + ## Release 6.7.0 - Removed requirement of `{type}` part in `indices.put_mapping`, see new API specification [here](https://github.com/elastic/elasticsearch/blob/v6.7.0/rest-api-spec/src/main/resources/rest-api-spec/api/indices.put_mapping.json) @@ -29,7 +37,7 @@ - [DOCS] Update `community.asciidoc`, added `ElasticSearchQueryDSL` project [#749](https://github.com/elastic/elasticsearch-php/pull/749) - [DOCS] Proper return type array for get method for `IndicesNamespace` [#651](https://github.com/elastic/elasticsearch-php/pull/651) - [DOCS] Fix full docs link [#862](https://github.com/elastic/elasticsearch-php/pull/862) -- [DOCS] Update breaking-changes.asciidoc, removal of ClientBuilder::defaultLogger() [879](https://github.com/elastic/elasticsearch-php/pull/879) +- [DOCS] Update breaking-changes.asciidoc, removal of ClientBuilder::defaultLogger() [#879](https://github.com/elastic/elasticsearch-php/pull/879) ### Testing diff --git a/src/Elasticsearch/Client.php b/src/Elasticsearch/Client.php index 1681fc5f6..73fe4339c 100644 --- a/src/Elasticsearch/Client.php +++ b/src/Elasticsearch/Client.php @@ -34,6 +34,8 @@ */ class Client { + const VERSION = '6.7.1'; + /** * @var Transport */ @@ -155,6 +157,39 @@ public function ping($params = []) return true; } + /** + * $params['body'] = (string) The ranking evaluation search definition, including + * search requests, document ratings and ranking metric definition (Required) + * ['index'] = (list) A comma-separated list of index names to search; use `_all` or + * empty string to perform the operation on all indices + * ['ignore_unavailable'] = (boolean) Whether specified concrete indices should be + * ignored when unavailable (missing or closed) + * ['allow_no_indices'] = (boolean) Whether to ignore if a wildcard indices expression + * resolves into no concrete indices. (This includes `_all` string or when no indices have been specified) + * ['expand_wildcards'] = (enum) Whether to expand wildcard expression to concrete indices that are open, + * closed or both. + * + * @return callable|array + */ + public function rankEval(array $params) + { + $body = $this->extractArgument($params, 'body'); + $index = $this->extractArgument($params, 'index'); + /** + * @var callable $endpointBuilder +*/ + $endpointBuilder = $this->endpoints; + /** + * @var \Elasticsearch\Endpoints\RankEval $endpoint +*/ + $endpoint = $endpointBuilder('RankEval'); + $endpoint->setBody($body) + ->setIndex($index); + $endpoint->setParams($params); + + return $this->performRequest($endpoint); + } + /** * $params['id'] = (string) The document ID (Required) * ['index'] = (string) The name of the index (Required) @@ -1021,6 +1056,27 @@ public function scroll($params = array()) return $this->performRequest($endpoint); } + /** + * $params['body'] = (string) The script to execute + * + * @return callable|array + */ + public function scriptsPainlessExecute(array $params = []) + { + $body = $this->extractArgument($params, 'body'); + /** + * @var callable $endpointBuilder +*/ + $endpointBuilder = $this->endpoints; + /** + * @var \Elasticsearch\Endpoints\ScriptsPainlessExecute $endpoint +*/ + $endpoint = $endpointBuilder('ScriptsPainlessExecute'); + $endpoint->setBody($body); + $endpoint->setParams($params); + return $this->performRequest($endpoint); + } + /** * $params['scroll_id'] = (string) The scroll ID for scrolled search * ['scroll'] = (duration) Specify how long a consistent view of the index should be maintained for scrolled search diff --git a/src/Elasticsearch/Connections/Connection.php b/src/Elasticsearch/Connections/Connection.php index b31de569f..928cefe05 100644 --- a/src/Elasticsearch/Connections/Connection.php +++ b/src/Elasticsearch/Connections/Connection.php @@ -4,6 +4,7 @@ namespace Elasticsearch\Connections; +use Elasticsearch\Client; use Elasticsearch\Common\Exceptions\AlreadyExpiredException; use Elasticsearch\Common\Exceptions\BadRequest400Exception; use Elasticsearch\Common\Exceptions\Conflict409Exception; @@ -126,6 +127,15 @@ public function __construct( unset($connectionParams['client']['headers']); } + // Add the User-Agent using the format: / (metadata-values) + $this->headers['User-Agent'] = [sprintf( + "elasticsearch-php/%s (%s %s, PHP %s)", + Client::VERSION, + php_uname("s"), + php_uname("r"), + phpversion() + )]; + $host = $hostDetails['host'].':'.$hostDetails['port']; $path = null; if (isset($hostDetails['path']) === true) { @@ -179,6 +189,12 @@ public function performRequest($method, $uri, $params = null, $body = null, $opt return $future; } + /** @return array */ + public function getHeaders() + { + return $this->headers; + } + /** @return string */ public function getTransportSchema() { diff --git a/src/Elasticsearch/Endpoints/Cluster/AllocationExplain.php b/src/Elasticsearch/Endpoints/Cluster/AllocationExplain.php index 429b5d0f4..51e78c8f8 100644 --- a/src/Elasticsearch/Endpoints/Cluster/AllocationExplain.php +++ b/src/Elasticsearch/Endpoints/Cluster/AllocationExplain.php @@ -59,6 +59,6 @@ public function getParamWhitelist() */ public function getMethod() { - return 'GET'; + return isset($this->body) ? 'POST' : 'GET'; } } diff --git a/src/Elasticsearch/Endpoints/Count.php b/src/Elasticsearch/Endpoints/Count.php index e35e9938b..62f4ffec2 100644 --- a/src/Elasticsearch/Endpoints/Count.php +++ b/src/Elasticsearch/Endpoints/Count.php @@ -84,6 +84,6 @@ public function getParamWhitelist() */ public function getMethod() { - return 'GET'; + return isset($this->body) ? 'POST' : 'GET'; } } diff --git a/src/Elasticsearch/Endpoints/Explain.php b/src/Elasticsearch/Endpoints/Explain.php index 2c89d67d2..26963cee3 100644 --- a/src/Elasticsearch/Endpoints/Explain.php +++ b/src/Elasticsearch/Endpoints/Explain.php @@ -99,6 +99,6 @@ public function getParamWhitelist() */ public function getMethod() { - return 'GET'; + return isset($this->body) ? 'POST' : 'GET'; } } diff --git a/src/Elasticsearch/Endpoints/FieldCaps.php b/src/Elasticsearch/Endpoints/FieldCaps.php index f9ae24d9e..0c8f55c8b 100644 --- a/src/Elasticsearch/Endpoints/FieldCaps.php +++ b/src/Elasticsearch/Endpoints/FieldCaps.php @@ -66,6 +66,6 @@ public function getParamWhitelist() */ public function getMethod() { - return 'GET'; + return isset($this->body) ? 'POST' : 'GET'; } } diff --git a/src/Elasticsearch/Endpoints/Indices/Analyze.php b/src/Elasticsearch/Endpoints/Indices/Analyze.php index ddcac13cd..4e918c35d 100644 --- a/src/Elasticsearch/Endpoints/Indices/Analyze.php +++ b/src/Elasticsearch/Endpoints/Indices/Analyze.php @@ -76,6 +76,6 @@ public function getParamWhitelist() */ public function getMethod() { - return 'GET'; + return isset($this->body) ? 'POST' : 'GET'; } } diff --git a/src/Elasticsearch/Endpoints/Indices/ClearCache.php b/src/Elasticsearch/Endpoints/Indices/ClearCache.php index 1d7bce8cc..0e3a7987d 100644 --- a/src/Elasticsearch/Endpoints/Indices/ClearCache.php +++ b/src/Elasticsearch/Endpoints/Indices/ClearCache.php @@ -59,6 +59,6 @@ public function getParamWhitelist() */ public function getMethod() { - return 'GET'; + return isset($this->body) ? 'POST' : 'GET'; } } diff --git a/src/Elasticsearch/Endpoints/Indices/Flush.php b/src/Elasticsearch/Endpoints/Indices/Flush.php index 90626ab58..97912e87b 100644 --- a/src/Elasticsearch/Endpoints/Indices/Flush.php +++ b/src/Elasticsearch/Endpoints/Indices/Flush.php @@ -63,6 +63,6 @@ public function getParamWhitelist() */ public function getMethod() { - return 'GET'; + return isset($this->body) ? 'POST' : 'GET'; } } diff --git a/src/Elasticsearch/Endpoints/Indices/ValidateQuery.php b/src/Elasticsearch/Endpoints/Indices/ValidateQuery.php index 6a7cd57cc..1a9acde37 100644 --- a/src/Elasticsearch/Endpoints/Indices/ValidateQuery.php +++ b/src/Elasticsearch/Endpoints/Indices/ValidateQuery.php @@ -74,6 +74,6 @@ public function getParamWhitelist() */ public function getMethod() { - return 'GET'; + return isset($this->body) ? 'POST' : 'GET'; } } diff --git a/src/Elasticsearch/Endpoints/Ingest/Simulate.php b/src/Elasticsearch/Endpoints/Ingest/Simulate.php index 16889ab5f..962713b55 100644 --- a/src/Elasticsearch/Endpoints/Ingest/Simulate.php +++ b/src/Elasticsearch/Endpoints/Ingest/Simulate.php @@ -62,6 +62,6 @@ public function getParamWhitelist() */ public function getMethod() { - return 'GET'; + return isset($this->body) ? 'POST' : 'GET'; } } diff --git a/src/Elasticsearch/Endpoints/MTermVectors.php b/src/Elasticsearch/Endpoints/MTermVectors.php index 00b69c259..93cd432b9 100644 --- a/src/Elasticsearch/Endpoints/MTermVectors.php +++ b/src/Elasticsearch/Endpoints/MTermVectors.php @@ -67,6 +67,6 @@ public function getParamWhitelist() */ public function getMethod() { - return 'POST'; + return isset($this->body) ? 'POST' : 'GET'; } } diff --git a/src/Elasticsearch/Endpoints/Msearch.php b/src/Elasticsearch/Endpoints/Msearch.php index 2beaa31b8..48c0cd0e6 100644 --- a/src/Elasticsearch/Endpoints/Msearch.php +++ b/src/Elasticsearch/Endpoints/Msearch.php @@ -104,6 +104,6 @@ public function getBody() */ public function getMethod() { - return 'GET'; + return isset($this->body) ? 'POST' : 'GET'; } } diff --git a/src/Elasticsearch/Endpoints/MsearchTemplate.php b/src/Elasticsearch/Endpoints/MsearchTemplate.php index efe440064..4c233921d 100644 --- a/src/Elasticsearch/Endpoints/MsearchTemplate.php +++ b/src/Elasticsearch/Endpoints/MsearchTemplate.php @@ -102,6 +102,6 @@ public function getBody() */ public function getMethod() { - return 'GET'; + return isset($this->body) ? 'POST' : 'GET'; } } diff --git a/src/Elasticsearch/Endpoints/RankEval.php b/src/Elasticsearch/Endpoints/RankEval.php new file mode 100644 index 000000000..6452cdaa3 --- /dev/null +++ b/src/Elasticsearch/Endpoints/RankEval.php @@ -0,0 +1,61 @@ + + * @license http://www.apache.org/licenses/LICENSE-2.0 Apache2 + * @link http://elastic.co + */ +class RankEval extends AbstractEndpoint +{ + /** + * @return array + */ + public function getParamWhitelist() + { + return [ + 'ignore_unavailable', + 'allow_no_indices', + 'expand_wildcards' + ]; + } + + /** + * @return string + */ + public function getURI() + { + $index = $this->index ?? null; + if (isset($index)) { + return "/$index/_rank_eval"; + } + return '/_rank_eval'; + } + + /** + * @return string + */ + public function getMethod() + { + return 'POST'; + } + + /** + * @param array $body + * @return $this + */ + public function setBody($body) + { + if (isset($body) !== true) { + return $this; + } + $this->body = $body; + return $this; + } +} diff --git a/src/Elasticsearch/Endpoints/RenderSearchTemplate.php b/src/Elasticsearch/Endpoints/RenderSearchTemplate.php index 559226189..87d7ebef7 100644 --- a/src/Elasticsearch/Endpoints/RenderSearchTemplate.php +++ b/src/Elasticsearch/Endpoints/RenderSearchTemplate.php @@ -74,6 +74,6 @@ public function getBody() */ public function getMethod() { - return 'GET'; + return isset($this->body) ? 'POST' : 'GET'; } } diff --git a/src/Elasticsearch/Endpoints/ScriptsPainlessExecute.php b/src/Elasticsearch/Endpoints/ScriptsPainlessExecute.php new file mode 100644 index 000000000..f690636f2 --- /dev/null +++ b/src/Elasticsearch/Endpoints/ScriptsPainlessExecute.php @@ -0,0 +1,58 @@ + + * @license http://www.apache.org/licenses/LICENSE-2.0 Apache2 + * @link http://elastic.co + */ +class ScriptsPainlessExecute extends AbstractEndpoint +{ + /** + * @return array + */ + public function getParamWhitelist() + { + return []; + } + + /** + * @return string + */ + public function getURI() + { + return "/_scripts/painless/_execute"; + } + + /** + * @return string + */ + public function getMethod() + { + return isset($this->body) ? 'POST' : 'GET'; + } + + /** + * @param array $body + * @return $this + */ + public function setBody($body) + { + if (isset($body) !== true) { + return $this; + } + + $this->body = $body; + + return $this; + } +} diff --git a/src/Elasticsearch/Endpoints/Scroll.php b/src/Elasticsearch/Endpoints/Scroll.php index 7ab720537..9aed3e69f 100644 --- a/src/Elasticsearch/Endpoints/Scroll.php +++ b/src/Elasticsearch/Endpoints/Scroll.php @@ -100,6 +100,6 @@ public function getParamWhitelist() */ public function getMethod() { - return 'GET'; + return isset($this->body) ? 'POST' : 'GET'; } } diff --git a/src/Elasticsearch/Endpoints/Search.php b/src/Elasticsearch/Endpoints/Search.php index ad35a750b..15bc9e41e 100644 --- a/src/Elasticsearch/Endpoints/Search.php +++ b/src/Elasticsearch/Endpoints/Search.php @@ -115,6 +115,6 @@ public function getParamWhitelist() */ public function getMethod() { - return 'GET'; + return isset($this->body) ? 'POST' : 'GET'; } } diff --git a/src/Elasticsearch/Endpoints/SearchTemplate.php b/src/Elasticsearch/Endpoints/SearchTemplate.php index f5298f3a1..776aadb7a 100644 --- a/src/Elasticsearch/Endpoints/SearchTemplate.php +++ b/src/Elasticsearch/Endpoints/SearchTemplate.php @@ -76,6 +76,6 @@ public function getParamWhitelist() */ public function getMethod() { - return 'GET'; + return isset($this->body) ? 'POST' : 'GET'; } } diff --git a/src/Elasticsearch/Endpoints/TermVectors.php b/src/Elasticsearch/Endpoints/TermVectors.php index 26058107f..a461105be 100644 --- a/src/Elasticsearch/Endpoints/TermVectors.php +++ b/src/Elasticsearch/Endpoints/TermVectors.php @@ -92,6 +92,6 @@ public function getParamWhitelist() */ public function getMethod() { - return 'POST'; + return isset($this->body) ? 'POST' : 'GET'; } } diff --git a/src/Elasticsearch/Endpoints/UpdateByQuery.php b/src/Elasticsearch/Endpoints/UpdateByQuery.php index 686218858..565a4e01f 100644 --- a/src/Elasticsearch/Endpoints/UpdateByQuery.php +++ b/src/Elasticsearch/Endpoints/UpdateByQuery.php @@ -105,11 +105,14 @@ public function getParamWhitelist() 'version', 'version_type', 'request_cache', + 'request_per_second', + 'slices', 'refresh', 'consistency', 'scroll_size', 'wait_for_completion', - 'pipeline', + 'wait_for_active_shards', + 'pipeline' ]; } diff --git a/tests/Elasticsearch/Tests/Connections/ConnectionTest.php b/tests/Elasticsearch/Tests/Connections/ConnectionTest.php new file mode 100644 index 000000000..9e91648db --- /dev/null +++ b/tests/Elasticsearch/Tests/Connections/ConnectionTest.php @@ -0,0 +1,80 @@ +logger = $this->createMock(LoggerInterface::class); + $this->trace = $this->createMock(LoggerInterface::class); + $this->serializer = $this->createMock(SerializerInterface::class); + } + + public function testConstructor() + { + $params = []; + $host = [ + 'host' => 'localhost' + ]; + $connection = new Connection( + function () { + }, + $host, + $params, + $this->serializer, + $this->logger, + $this->trace + ); + $this->assertInstanceOf(Connection::class, $connection); + } + + public function testGetHeadersContainUserAgent() + { + $params = []; + $host = [ + 'host' => 'localhost' + ]; + $connection = new Connection( + function () { + }, + $host, + $params, + $this->serializer, + $this->logger, + $this->trace + ); + $headers = $connection->getHeaders(); + $this->assertArrayHasKey('User-Agent', $headers); + $this->assertContains('elasticsearch-php/'. Client::VERSION, $headers['User-Agent'][0]); + } + + public function testUserAgentHeaderIsSent() + { + $params = []; + $host = [ + 'host' => 'localhost' + ]; + $connection = new Connection( + ClientBuilder::defaultHandler(), + $host, + $params, + $this->serializer, + $this->logger, + $this->trace + ); + $result = $connection->performRequest('GET', '/'); + $request = $connection->getLastRequestInfo()['request']; + $this->assertArrayHasKey('User-Agent', $request['headers']); + $this->assertContains('elasticsearch-php/'. Client::VERSION, $request['headers']['User-Agent'][0]); + } +}