diff --git a/src/Elasticsearch/Client.php b/src/Elasticsearch/Client.php index 9f6ed9b5c..f49aff5fa 100644 --- a/src/Elasticsearch/Client.php +++ b/src/Elasticsearch/Client.php @@ -34,6 +34,8 @@ */ class Client { + const VERSION = '6.5.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) @@ -1011,6 +1046,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 1c668e8a6..4888db657 100644 --- a/src/Elasticsearch/Endpoints/Explain.php +++ b/src/Elasticsearch/Endpoints/Explain.php @@ -97,6 +97,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 2fba78aec..45f4e73e2 100644 --- a/src/Elasticsearch/Endpoints/Msearch.php +++ b/src/Elasticsearch/Endpoints/Msearch.php @@ -103,6 +103,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 0bb31ac9c..d68c7324b 100644 --- a/src/Elasticsearch/Endpoints/Scroll.php +++ b/src/Elasticsearch/Endpoints/Scroll.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/Search.php b/src/Elasticsearch/Endpoints/Search.php index b223e129f..598aad103 100644 --- a/src/Elasticsearch/Endpoints/Search.php +++ b/src/Elasticsearch/Endpoints/Search.php @@ -110,6 +110,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 bd2e5a8ee..24411752c 100644 --- a/src/Elasticsearch/Endpoints/UpdateByQuery.php +++ b/src/Elasticsearch/Endpoints/UpdateByQuery.php @@ -103,11 +103,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..8906e34df --- /dev/null +++ b/tests/Elasticsearch/Tests/Connections/ConnectionTest.php @@ -0,0 +1,78 @@ +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]); + } +}