Skip to content

Commit

Permalink
Re-Add the DeleteByQuery Functionality (#513)
Browse files Browse the repository at this point in the history
* Re-Add the DeleteByQuery Functionality

Reverts 9f3776a and brings the
`DeleteByQuery` endpoint up to spec with the current API.

* Remove `slice` as a Parameter from DeleteByQuery

It's in the request body, not the query string.

* Include All the DeleteByQuery Params

Generated from a bit of scripting around the delete by query api spec.
  • Loading branch information
chrisguitarguy authored and polyfractal committed Dec 16, 2016
1 parent f32cc54 commit b262dca
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/Elasticsearch/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,67 @@ public function delete($params)
return $this->performRequest($endpoint);
}

/**
*
* $params['_source'] = (list) True or false to return the _source field or not, or a list of fields to return
* ['_source_exclude'] = (array) A list of fields to exclude from the returned _source field
* ['_source_include'] = (array) A list of fields to extract and return from the _source field
* ['allow_no_indices'] = (bool) Whether to ignore if a wildcard indices expression resolves into no concrete indices. (This includes `_all` string or when no indices have been specified)
* ['analyze_wildcard'] = (bool) Specify whether wildcard and prefix queries should be analyzed (default: false)
* ['analyzer'] = (string) The analyzer to use for the query string
* ['conflicts'] = (enum) What to do when the delete-by-query hits version conflicts?
* ['default_operator'] = (enum) The default operator for query string query (AND or OR)
* ['df'] = (string) The field to use as default where no field prefix is given in the query string
* ['expand_wildcards'] = (enum) Whether to expand wildcard expression to concrete indices that are open, closed or both.
* ['from'] = (number) Starting offset (default: 0)
* ['ignore_unavailable'] = (bool) Whether specified concrete indices should be ignored when unavailable (missing or closed)
* ['lenient'] = (bool) Specify whether format-based query failures (such as providing text to a numeric field) should be ignored
* ['preference'] = (string) Specify the node or shard the operation should be performed on (default: random)
* ['q'] = (string) Query in the Lucene query string syntax
* ['refresh'] = (bool) Should the effected indexes be refreshed?
* ['request_cache'] = (bool) Specify if request cache should be used for this request or not, defaults to index level setting
* ['requests_per_second'] = (number) The throttle for this request in sub-requests per second. -1 means no throttle.
* ['routing'] = (array) A comma-separated list of specific routing values
* ['scroll'] = (number) Specify how long a consistent view of the index should be maintained for scrolled search
* ['scroll_size'] = (number) Size on the scroll request powering the update_by_query
* ['search_timeout'] = (number) Explicit timeout for each search request. Defaults to no timeout.
* ['search_type'] = (enum) Search operation type
* ['size'] = (number) Number of hits to return (default: 10)
* ['slices'] = (integer) The number of slices this task should be divided into. Defaults to 1 meaning the task isn't sliced into subtasks.
* ['sort'] = (array) A comma-separated list of <field>:<direction> pairs
* ['stats'] = (array) Specific 'tag' of the request for logging and statistical purposes
* ['terminate_after'] = (number) The maximum number of documents to collect for each shard, upon reaching which the query execution will terminate early.
* ['timeout'] = (number) Time each individual bulk request should wait for shards that are unavailable.
* ['version'] = (bool) Specify whether to return document version as part of a hit
* ['wait_for_active_shards'] = (string) Sets the number of shard copies that must be active before proceeding with the delete by query operation. Defaults to 1, meaning the primary shard only. Set to `all` for all shard copies, otherwise set to any non-negative value less than or equal to the total number of copies for the shard (number of replicas + 1)
* ['wait_for_completion'] = (bool) Should the request should block until the delete-by-query is complete.
*
* @param array $params
*
* @return array
*/
public function deleteByQuery($params = array())
{
$index = $this->extractArgument($params, 'index');

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

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

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

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

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

/**
* $params['index'] = (list) A comma-separated list of indices to restrict the results
* ['type'] = (list) A comma-separated list of types to restrict the results
Expand Down
103 changes: 103 additions & 0 deletions src/Elasticsearch/Endpoints/DeleteByQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace Elasticsearch\Endpoints;

use Elasticsearch\Common\Exceptions;

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

$this->body = $body;

return $this;
}

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

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

return $uri;
}

/**
* @return string[]
*/
protected function getParamWhitelist()
{
return array(
'_source',
'_source_exclude',
'_source_include',
'allow_no_indices',
'analyze_wildcard',
'analyzer',
'conflicts',
'default_operator',
'df',
'expand_wildcards',
'from',
'ignore_unavailable',
'lenient',
'preference',
'q',
'refresh',
'request_cache',
'requests_per_second',
'routing',
'scroll',
'scroll_size',
'search_timeout',
'search_type',
'size',
'slices',
'sort',
'stats',
'terminate_after',
'timeout',
'version',
'wait_for_active_shards',
'wait_for_completion',
);
}

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

0 comments on commit b262dca

Please sign in to comment.