Skip to content

Commit

Permalink
Put back UpdateByQuery (#531)
Browse files Browse the repository at this point in the history
  • Loading branch information
sandyandi authored and polyfractal committed Jan 26, 2017
1 parent 4aba49b commit 6f4394f
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ You'll notice that the installation command specified `--no-dev`. This prevents

PHP Version Requirement
----
VeVersion 5.0 of this library requires at least PHP version 5.6.6 to function. In addition, it requires the native JSON
Version 5.0 of this library requires at least PHP version 5.6.6 to function. In addition, it requires the native JSON
extension to be version 1.3.7 or higher.

| PHP Version | Elasticsearch-PHP Branch |
Expand Down
2 changes: 2 additions & 0 deletions docs/quickstart.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ php composer.phar install --no-dev
----------------------------
require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$client = ClientBuilder::create()->build();
----------------------------

Expand Down
96 changes: 94 additions & 2 deletions src/Elasticsearch/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,8 @@ public function deleteByQuery($params = array())
->setType($type)
->setBody($body);
$endpoint->setParams($params);
$response = $endpoint->performRequest();

return $endpoint->resultOrFuture($response);
return $this->performRequest($endpoint);
}

/**
Expand Down Expand Up @@ -888,6 +887,7 @@ public function explain($params)
* ['suggest_size'] = (number) How many suggestions to return in response
* ['suggest_text'] = (text) The source text for which the suggestions should be returned
* ['timeout'] = (time) Explicit operation timeout
* ['terminate_after'] = (number) The maximum number of documents to collect for each shard, upon reaching which the query execution will terminate early.
* ['version'] = (boolean) Specify whether to return document version as part of a hit
* ['body'] = (array|string) The search definition using the Query DSL
*
Expand Down Expand Up @@ -1069,6 +1069,98 @@ public function update($params)
return $this->performRequest($endpoint);
}

/**
* $params['index'] = (list) A comma-separated list of index names to search; use `_all` or
* empty string to perform the operation on all indices (Required)
* ['type'] = (list) A comma-separated list of document types to search; leave empty to
* perform the operation on all types
* ['analyzer'] = (string) The analyzer to use for the query string
* ['analyze_wildcard'] = (boolean) Specify whether wildcard and prefix queries should be analyzed
* (default: false)
* ['default_operator'] = (enum) The default operator for query string query (AND or OR) (AND,OR)
* (default: OR)
* ['df'] = (string) The field to use as default where no field prefix is given in the
* query string
* ['explain'] = (boolean) Specify whether to return detailed information about score
* computation as part of a hit
* ['fields'] = (list) A comma-separated list of fields to return as part of a hit
* ['fielddata_fields'] = (list) A comma-separated list of fields to return as the field data
* representation of a field for each hit
* ['from'] = (number) Starting offset (default: 0)
* ['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)
* ['conflicts'] = (enum) What to do when the reindex hits version conflicts? (abort,proceed)
* (default: abort)
* ['expand_wildcards'] = (enum) Whether to expand wildcard expression to concrete indices that are
* open, closed or both. (open,closed,none,all) (default: open)
* ['lenient'] = (boolean) Specify whether format-based query failures (such as providing
* text to a numeric field) should be ignored
* ['lowercase_expanded_terms'] = (boolean) Specify whether query terms should be lowercased
* ['preference'] = (string) Specify the node or shard the operation should be performed on
* (default: random)
* ['q'] = (string) Query in the Lucene query string syntax
* ['routing'] = (list) A comma-separated list of specific routing values
* ['scroll'] = (duration) Specify how long a consistent view of the index should be
* maintained for scrolled search
* ['search_type'] = (enum) Search operation type (query_then_fetch,dfs_query_then_fetch)
* ['search_timeout'] = (time) Explicit timeout for each search request. Defaults to no timeout.
* ['size'] = (number) Number of hits to return (default: 10)
* ['sort'] = (list) A comma-separated list of <field>:<direction> pairs
* ['_source'] = (list) True or false to return the _source field or not, or a list of
* fields to return
* ['_source_exclude'] = (list) A list of fields to exclude from the returned _source field
* ['_source_include'] = (list) A list of fields to extract and return from the _source field
* ['terminate_after'] = (number) The maximum number of documents to collect for each shard, upon
* reaching which the query execution will terminate early.
* ['stats'] = (list) Specific 'tag' of the request for logging and statistical purposes
* ['suggest_field'] = (string) Specify which field to use for suggestions
* ['suggest_mode'] = (enum) Specify suggest mode (missing,popular,always) (default: missing)
* ['suggest_size'] = (number) How many suggestions to return in response
* ['suggest_text'] = (text) The source text for which the suggestions should be returned
* ['timeout'] = (time) Time each individual bulk request should wait for shards that are
* unavailable. (default: 1m)
* ['track_scores'] = (boolean) Whether to calculate and return scores even if they are not used
* for sorting
* ['version'] = (boolean) Specify whether to return document version as part of a hit
* ['version_type'] = (boolean) Should the document increment the version number (internal) on
* hit or not (reindex)
* ['request_cache'] = (boolean) Specify if request cache should be used for this request or not,
* defaults to index level setting
* ['refresh'] = (boolean) Should the effected indexes be refreshed?
* ['consistency'] = (enum) Explicit write consistency setting for the operation
* (one,quorum,all)
* ['scroll_size'] = (integer) Size on the scroll request powering the update_by_query
* ['wait_for_completion'] = (boolean) Should the request should block until the reindex is complete.
* (default: false)
* ['body'] = The search definition using the Query DSL
*
* @param array $params
*
* @return array
*/
public function updateByQuery($params = array())
{
$index = $this->extractArgument($params, 'index');

$body = $this->extractArgument($params, 'body');

$type = $this->extractArgument($params, 'type');

/** @var callback $endpointBuilder */
$endpointBuilder = $this->endpoints;

/** @var \Elasticsearch\Endpoints\UpdateByQuery $endpoint */
$endpoint = $endpointBuilder('UpdateByQuery');
$endpoint->setIndex($index)
->setType($type)
->setBody($body);
$endpoint->setParams($params);

return $this->performRequest($endpoint);
}

/**
* $params['id'] = (string) The script ID (Required)
* ['lang'] = (string) The script language (Required)
Expand Down
8 changes: 4 additions & 4 deletions src/Elasticsearch/Endpoints/DeleteByQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function setBody($body)
* @throws \Elasticsearch\Common\Exceptions\RuntimeException
* @return string
*/
protected function getURI()
public function getURI()
{
if (!$this->index) {
throw new Exceptions\RuntimeException(
Expand All @@ -55,7 +55,7 @@ protected function getURI()
/**
* @return string[]
*/
protected function getParamWhitelist()
public function getParamWhitelist()
{
return array(
'_source',
Expand All @@ -72,7 +72,7 @@ protected function getParamWhitelist()
'ignore_unavailable',
'lenient',
'preference',
'q',
'query',
'refresh',
'request_cache',
'requests_per_second',
Expand All @@ -96,7 +96,7 @@ protected function getParamWhitelist()
/**
* @return string
*/
protected function getMethod()
public function getMethod()
{
return 'POST';
}
Expand Down
3 changes: 2 additions & 1 deletion src/Elasticsearch/Endpoints/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ public function getParamWhitelist()
'version',
'fielddata_fields',
'docvalue_fields',
'filter_path'
'filter_path',
'terminate_after',
);
}

Expand Down
119 changes: 119 additions & 0 deletions src/Elasticsearch/Endpoints/UpdateByQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace Elasticsearch\Endpoints;

use Elasticsearch\Common\Exceptions;

/**
* Class UpdateByQuery
*
* @category Elasticsearch
* @package Elasticsearch\Endpoints *
* @author Zachary Tong <[email protected]>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
* @link http://elasticsearch.org
*/
class UpdateByQuery extends AbstractEndpoint
{
/**
* @param array $body
*
* @throws \Elasticsearch\Common\Exceptions\InvalidArgumentException
* @return $this
*/
public function setBody($body)
{
if (isset($body) !== true) {
return $this;
}

if (is_array($body) !== true) {
throw new Exceptions\InvalidArgumentException(
'Body must be an array'
);
}
$this->body = $body;

return $this;
}


/**
* @throws \Elasticsearch\Common\Exceptions\BadMethodCallException
* @return string
*/
public function getURI()
{
if (!$this->index) {
throw new Exceptions\RuntimeException(
'index is required for UpdateByQuery'
);
}

$uri = "/{$this->index}/_update_by_query";
if ($this->type) {
$uri = "/{$this->index}/{$this->type}/_update_by_query";
}

return $uri;
}


/**
* @return string[]
*/
public function getParamWhitelist()
{
return [
'analyzer',
'analyze_wildcard',
'default_operator',
'df',
'explain',
'fields',
'fielddata_fields',
'from',
'ignore_unavailable',
'allow_no_indices',
'conflicts',
'expand_wildcards',
'lenient',
'lowercase_expanded_terms',
'preference',
'q',
'routing',
'scroll',
'search_type',
'search_timeout',
'size',
'sort',
'_source',
'_source_exclude',
'_source_include',
'terminate_after',
'stats',
'suggest_field',
'suggest_mode',
'suggest_size',
'suggest_text',
'timeout',
'track_scores',
'version',
'version_type',
'request_cache',
'refresh',
'consistency',
'scroll_size',
'wait_for_completion',
];
}


/**
* @return string
*/
public function getMethod()
{
return 'POST';
}
}

0 comments on commit 6f4394f

Please sign in to comment.