Skip to content

Commit b262dca

Browse files
chrisguitarguypolyfractal
authored andcommitted
Re-Add the DeleteByQuery Functionality (#513)
* 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.
1 parent f32cc54 commit b262dca

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed

src/Elasticsearch/Client.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,67 @@ public function delete($params)
253253
return $this->performRequest($endpoint);
254254
}
255255

256+
/**
257+
*
258+
* $params['_source'] = (list) True or false to return the _source field or not, or a list of fields to return
259+
* ['_source_exclude'] = (array) A list of fields to exclude from the returned _source field
260+
* ['_source_include'] = (array) A list of fields to extract and return from the _source field
261+
* ['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)
262+
* ['analyze_wildcard'] = (bool) Specify whether wildcard and prefix queries should be analyzed (default: false)
263+
* ['analyzer'] = (string) The analyzer to use for the query string
264+
* ['conflicts'] = (enum) What to do when the delete-by-query hits version conflicts?
265+
* ['default_operator'] = (enum) The default operator for query string query (AND or OR)
266+
* ['df'] = (string) The field to use as default where no field prefix is given in the query string
267+
* ['expand_wildcards'] = (enum) Whether to expand wildcard expression to concrete indices that are open, closed or both.
268+
* ['from'] = (number) Starting offset (default: 0)
269+
* ['ignore_unavailable'] = (bool) Whether specified concrete indices should be ignored when unavailable (missing or closed)
270+
* ['lenient'] = (bool) Specify whether format-based query failures (such as providing text to a numeric field) should be ignored
271+
* ['preference'] = (string) Specify the node or shard the operation should be performed on (default: random)
272+
* ['q'] = (string) Query in the Lucene query string syntax
273+
* ['refresh'] = (bool) Should the effected indexes be refreshed?
274+
* ['request_cache'] = (bool) Specify if request cache should be used for this request or not, defaults to index level setting
275+
* ['requests_per_second'] = (number) The throttle for this request in sub-requests per second. -1 means no throttle.
276+
* ['routing'] = (array) A comma-separated list of specific routing values
277+
* ['scroll'] = (number) Specify how long a consistent view of the index should be maintained for scrolled search
278+
* ['scroll_size'] = (number) Size on the scroll request powering the update_by_query
279+
* ['search_timeout'] = (number) Explicit timeout for each search request. Defaults to no timeout.
280+
* ['search_type'] = (enum) Search operation type
281+
* ['size'] = (number) Number of hits to return (default: 10)
282+
* ['slices'] = (integer) The number of slices this task should be divided into. Defaults to 1 meaning the task isn't sliced into subtasks.
283+
* ['sort'] = (array) A comma-separated list of <field>:<direction> pairs
284+
* ['stats'] = (array) Specific 'tag' of the request for logging and statistical purposes
285+
* ['terminate_after'] = (number) The maximum number of documents to collect for each shard, upon reaching which the query execution will terminate early.
286+
* ['timeout'] = (number) Time each individual bulk request should wait for shards that are unavailable.
287+
* ['version'] = (bool) Specify whether to return document version as part of a hit
288+
* ['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)
289+
* ['wait_for_completion'] = (bool) Should the request should block until the delete-by-query is complete.
290+
*
291+
* @param array $params
292+
*
293+
* @return array
294+
*/
295+
public function deleteByQuery($params = array())
296+
{
297+
$index = $this->extractArgument($params, 'index');
298+
299+
$type = $this->extractArgument($params, 'type');
300+
301+
$body = $this->extractArgument($params, 'body');
302+
303+
/** @var callback $endpointBuilder */
304+
$endpointBuilder = $this->endpoints;
305+
306+
/** @var \Elasticsearch\Endpoints\DeleteByQuery $endpoint */
307+
$endpoint = $endpointBuilder('DeleteByQuery');
308+
$endpoint->setIndex($index)
309+
->setType($type)
310+
->setBody($body);
311+
$endpoint->setParams($params);
312+
$response = $endpoint->performRequest();
313+
314+
return $endpoint->resultOrFuture($response);
315+
}
316+
256317
/**
257318
* $params['index'] = (list) A comma-separated list of indices to restrict the results
258319
* ['type'] = (list) A comma-separated list of types to restrict the results
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace Elasticsearch\Endpoints;
4+
5+
use Elasticsearch\Common\Exceptions;
6+
7+
/**
8+
* Class Deletebyquery
9+
*
10+
* @category Elasticsearch
11+
* @package Elasticsearch\Endpoints
12+
* @author Zachary Tong <zach@elastic.co>
13+
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
14+
* @link http://elastic.co
15+
*/
16+
class DeleteByQuery extends AbstractEndpoint
17+
{
18+
/**
19+
* @param array $body
20+
*
21+
* @throws \Elasticsearch\Common\Exceptions\InvalidArgumentException
22+
* @return $this
23+
*/
24+
public function setBody($body)
25+
{
26+
if (isset($body) !== true) {
27+
return $this;
28+
}
29+
30+
$this->body = $body;
31+
32+
return $this;
33+
}
34+
35+
/**
36+
* @throws \Elasticsearch\Common\Exceptions\RuntimeException
37+
* @return string
38+
*/
39+
protected function getURI()
40+
{
41+
if (!$this->index) {
42+
throw new Exceptions\RuntimeException(
43+
'index is required for Deletebyquery'
44+
);
45+
}
46+
47+
$uri = "/{$this->index}/_delete_by_query";
48+
if ($this->type) {
49+
$uri = "/{$this->index}/{$this->type}/_delete_by_query";
50+
}
51+
52+
return $uri;
53+
}
54+
55+
/**
56+
* @return string[]
57+
*/
58+
protected function getParamWhitelist()
59+
{
60+
return array(
61+
'_source',
62+
'_source_exclude',
63+
'_source_include',
64+
'allow_no_indices',
65+
'analyze_wildcard',
66+
'analyzer',
67+
'conflicts',
68+
'default_operator',
69+
'df',
70+
'expand_wildcards',
71+
'from',
72+
'ignore_unavailable',
73+
'lenient',
74+
'preference',
75+
'q',
76+
'refresh',
77+
'request_cache',
78+
'requests_per_second',
79+
'routing',
80+
'scroll',
81+
'scroll_size',
82+
'search_timeout',
83+
'search_type',
84+
'size',
85+
'slices',
86+
'sort',
87+
'stats',
88+
'terminate_after',
89+
'timeout',
90+
'version',
91+
'wait_for_active_shards',
92+
'wait_for_completion',
93+
);
94+
}
95+
96+
/**
97+
* @return string
98+
*/
99+
protected function getMethod()
100+
{
101+
return 'POST';
102+
}
103+
}

0 commit comments

Comments
 (0)