Skip to content

Commit

Permalink
Fix 843,846,add User-Agent + RankEval + ScriptsPainlessExecute
Browse files Browse the repository at this point in the history
  • Loading branch information
ezimuel committed Jul 19, 2019
1 parent 5d5f418 commit acbc76d
Show file tree
Hide file tree
Showing 23 changed files with 290 additions and 18 deletions.
56 changes: 56 additions & 0 deletions src/Elasticsearch/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
*/
class Client
{
const VERSION = '6.5.1';

/**
* @var Transport
*/
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions src/Elasticsearch/Connections/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -126,6 +127,15 @@ public function __construct(
unset($connectionParams['client']['headers']);
}

// Add the User-Agent using the format: <client-repo-name>/<client-version> (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) {
Expand Down Expand Up @@ -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()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Elasticsearch/Endpoints/Cluster/AllocationExplain.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ public function getParamWhitelist()
*/
public function getMethod()
{
return 'GET';
return isset($this->body) ? 'POST' : 'GET';
}
}
2 changes: 1 addition & 1 deletion src/Elasticsearch/Endpoints/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ public function getParamWhitelist()
*/
public function getMethod()
{
return 'GET';
return isset($this->body) ? 'POST' : 'GET';
}
}
2 changes: 1 addition & 1 deletion src/Elasticsearch/Endpoints/Explain.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,6 @@ public function getParamWhitelist()
*/
public function getMethod()
{
return 'GET';
return isset($this->body) ? 'POST' : 'GET';
}
}
2 changes: 1 addition & 1 deletion src/Elasticsearch/Endpoints/FieldCaps.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ public function getParamWhitelist()
*/
public function getMethod()
{
return 'GET';
return isset($this->body) ? 'POST' : 'GET';
}
}
2 changes: 1 addition & 1 deletion src/Elasticsearch/Endpoints/Indices/Analyze.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ public function getParamWhitelist()
*/
public function getMethod()
{
return 'GET';
return isset($this->body) ? 'POST' : 'GET';
}
}
2 changes: 1 addition & 1 deletion src/Elasticsearch/Endpoints/Indices/ClearCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ public function getParamWhitelist()
*/
public function getMethod()
{
return 'GET';
return isset($this->body) ? 'POST' : 'GET';
}
}
2 changes: 1 addition & 1 deletion src/Elasticsearch/Endpoints/Indices/Flush.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ public function getParamWhitelist()
*/
public function getMethod()
{
return 'GET';
return isset($this->body) ? 'POST' : 'GET';
}
}
2 changes: 1 addition & 1 deletion src/Elasticsearch/Endpoints/Indices/ValidateQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,6 @@ public function getParamWhitelist()
*/
public function getMethod()
{
return 'GET';
return isset($this->body) ? 'POST' : 'GET';
}
}
2 changes: 1 addition & 1 deletion src/Elasticsearch/Endpoints/Ingest/Simulate.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ public function getParamWhitelist()
*/
public function getMethod()
{
return 'GET';
return isset($this->body) ? 'POST' : 'GET';
}
}
2 changes: 1 addition & 1 deletion src/Elasticsearch/Endpoints/MTermVectors.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ public function getParamWhitelist()
*/
public function getMethod()
{
return 'POST';
return isset($this->body) ? 'POST' : 'GET';
}
}
2 changes: 1 addition & 1 deletion src/Elasticsearch/Endpoints/Msearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,6 @@ public function getBody()
*/
public function getMethod()
{
return 'GET';
return isset($this->body) ? 'POST' : 'GET';
}
}
2 changes: 1 addition & 1 deletion src/Elasticsearch/Endpoints/MsearchTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,6 @@ public function getBody()
*/
public function getMethod()
{
return 'GET';
return isset($this->body) ? 'POST' : 'GET';
}
}
61 changes: 61 additions & 0 deletions src/Elasticsearch/Endpoints/RankEval.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
declare(strict_types = 1);

namespace Elasticsearch\Endpoints;

/**
* Class RankEval
*
* @category Elasticsearch
* @package Elasticsearch\Endpoints
* @author Enrico Zimuel <[email protected]>
* @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;
}
}
2 changes: 1 addition & 1 deletion src/Elasticsearch/Endpoints/RenderSearchTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,6 @@ public function getBody()
*/
public function getMethod()
{
return 'GET';
return isset($this->body) ? 'POST' : 'GET';
}
}
58 changes: 58 additions & 0 deletions src/Elasticsearch/Endpoints/ScriptsPainlessExecute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types = 1);

namespace Elasticsearch\Endpoints;

use Elasticsearch\Common\Exceptions\RuntimeException;

/**
* Class Reindex
*
* @category Elasticsearch
* @package Elasticsearch\Endpoints
* @author Enrico Zimuel <[email protected]>
* @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;
}
}
2 changes: 1 addition & 1 deletion src/Elasticsearch/Endpoints/Scroll.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,6 @@ public function getParamWhitelist()
*/
public function getMethod()
{
return 'GET';
return isset($this->body) ? 'POST' : 'GET';
}
}
2 changes: 1 addition & 1 deletion src/Elasticsearch/Endpoints/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,6 @@ public function getParamWhitelist()
*/
public function getMethod()
{
return 'GET';
return isset($this->body) ? 'POST' : 'GET';
}
}
2 changes: 1 addition & 1 deletion src/Elasticsearch/Endpoints/SearchTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ public function getParamWhitelist()
*/
public function getMethod()
{
return 'GET';
return isset($this->body) ? 'POST' : 'GET';
}
}
2 changes: 1 addition & 1 deletion src/Elasticsearch/Endpoints/TermVectors.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,6 @@ public function getParamWhitelist()
*/
public function getMethod()
{
return 'POST';
return isset($this->body) ? 'POST' : 'GET';
}
}
5 changes: 4 additions & 1 deletion src/Elasticsearch/Endpoints/UpdateByQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
];
}

Expand Down
Loading

0 comments on commit acbc76d

Please sign in to comment.