Skip to content

Commit 6f4394f

Browse files
sandyandipolyfractal
authored andcommitted
Put back UpdateByQuery (#531)
1 parent 4aba49b commit 6f4394f

File tree

6 files changed

+222
-8
lines changed

6 files changed

+222
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ You'll notice that the installation command specified `--no-dev`. This prevents
8484

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

9090
| PHP Version | Elasticsearch-PHP Branch |

docs/quickstart.asciidoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ php composer.phar install --no-dev
3030
----------------------------
3131
require 'vendor/autoload.php';
3232
33+
use Elasticsearch\ClientBuilder;
34+
3335
$client = ClientBuilder::create()->build();
3436
----------------------------
3537

src/Elasticsearch/Client.php

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,8 @@ public function deleteByQuery($params = array())
313313
->setType($type)
314314
->setBody($body);
315315
$endpoint->setParams($params);
316-
$response = $endpoint->performRequest();
317316

318-
return $endpoint->resultOrFuture($response);
317+
return $this->performRequest($endpoint);
319318
}
320319

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

1072+
/**
1073+
* $params['index'] = (list) A comma-separated list of index names to search; use `_all` or
1074+
* empty string to perform the operation on all indices (Required)
1075+
* ['type'] = (list) A comma-separated list of document types to search; leave empty to
1076+
* perform the operation on all types
1077+
* ['analyzer'] = (string) The analyzer to use for the query string
1078+
* ['analyze_wildcard'] = (boolean) Specify whether wildcard and prefix queries should be analyzed
1079+
* (default: false)
1080+
* ['default_operator'] = (enum) The default operator for query string query (AND or OR) (AND,OR)
1081+
* (default: OR)
1082+
* ['df'] = (string) The field to use as default where no field prefix is given in the
1083+
* query string
1084+
* ['explain'] = (boolean) Specify whether to return detailed information about score
1085+
* computation as part of a hit
1086+
* ['fields'] = (list) A comma-separated list of fields to return as part of a hit
1087+
* ['fielddata_fields'] = (list) A comma-separated list of fields to return as the field data
1088+
* representation of a field for each hit
1089+
* ['from'] = (number) Starting offset (default: 0)
1090+
* ['ignore_unavailable'] = (boolean) Whether specified concrete indices should be ignored when
1091+
* unavailable (missing or closed)
1092+
* ['allow_no_indices'] = (boolean) Whether to ignore if a wildcard indices expression resolves into
1093+
* no concrete indices. (This includes `_all` string or when no indices have been specified)
1094+
* ['conflicts'] = (enum) What to do when the reindex hits version conflicts? (abort,proceed)
1095+
* (default: abort)
1096+
* ['expand_wildcards'] = (enum) Whether to expand wildcard expression to concrete indices that are
1097+
* open, closed or both. (open,closed,none,all) (default: open)
1098+
* ['lenient'] = (boolean) Specify whether format-based query failures (such as providing
1099+
* text to a numeric field) should be ignored
1100+
* ['lowercase_expanded_terms'] = (boolean) Specify whether query terms should be lowercased
1101+
* ['preference'] = (string) Specify the node or shard the operation should be performed on
1102+
* (default: random)
1103+
* ['q'] = (string) Query in the Lucene query string syntax
1104+
* ['routing'] = (list) A comma-separated list of specific routing values
1105+
* ['scroll'] = (duration) Specify how long a consistent view of the index should be
1106+
* maintained for scrolled search
1107+
* ['search_type'] = (enum) Search operation type (query_then_fetch,dfs_query_then_fetch)
1108+
* ['search_timeout'] = (time) Explicit timeout for each search request. Defaults to no timeout.
1109+
* ['size'] = (number) Number of hits to return (default: 10)
1110+
* ['sort'] = (list) A comma-separated list of <field>:<direction> pairs
1111+
* ['_source'] = (list) True or false to return the _source field or not, or a list of
1112+
* fields to return
1113+
* ['_source_exclude'] = (list) A list of fields to exclude from the returned _source field
1114+
* ['_source_include'] = (list) A list of fields to extract and return from the _source field
1115+
* ['terminate_after'] = (number) The maximum number of documents to collect for each shard, upon
1116+
* reaching which the query execution will terminate early.
1117+
* ['stats'] = (list) Specific 'tag' of the request for logging and statistical purposes
1118+
* ['suggest_field'] = (string) Specify which field to use for suggestions
1119+
* ['suggest_mode'] = (enum) Specify suggest mode (missing,popular,always) (default: missing)
1120+
* ['suggest_size'] = (number) How many suggestions to return in response
1121+
* ['suggest_text'] = (text) The source text for which the suggestions should be returned
1122+
* ['timeout'] = (time) Time each individual bulk request should wait for shards that are
1123+
* unavailable. (default: 1m)
1124+
* ['track_scores'] = (boolean) Whether to calculate and return scores even if they are not used
1125+
* for sorting
1126+
* ['version'] = (boolean) Specify whether to return document version as part of a hit
1127+
* ['version_type'] = (boolean) Should the document increment the version number (internal) on
1128+
* hit or not (reindex)
1129+
* ['request_cache'] = (boolean) Specify if request cache should be used for this request or not,
1130+
* defaults to index level setting
1131+
* ['refresh'] = (boolean) Should the effected indexes be refreshed?
1132+
* ['consistency'] = (enum) Explicit write consistency setting for the operation
1133+
* (one,quorum,all)
1134+
* ['scroll_size'] = (integer) Size on the scroll request powering the update_by_query
1135+
* ['wait_for_completion'] = (boolean) Should the request should block until the reindex is complete.
1136+
* (default: false)
1137+
* ['body'] = The search definition using the Query DSL
1138+
*
1139+
* @param array $params
1140+
*
1141+
* @return array
1142+
*/
1143+
public function updateByQuery($params = array())
1144+
{
1145+
$index = $this->extractArgument($params, 'index');
1146+
1147+
$body = $this->extractArgument($params, 'body');
1148+
1149+
$type = $this->extractArgument($params, 'type');
1150+
1151+
/** @var callback $endpointBuilder */
1152+
$endpointBuilder = $this->endpoints;
1153+
1154+
/** @var \Elasticsearch\Endpoints\UpdateByQuery $endpoint */
1155+
$endpoint = $endpointBuilder('UpdateByQuery');
1156+
$endpoint->setIndex($index)
1157+
->setType($type)
1158+
->setBody($body);
1159+
$endpoint->setParams($params);
1160+
1161+
return $this->performRequest($endpoint);
1162+
}
1163+
10721164
/**
10731165
* $params['id'] = (string) The script ID (Required)
10741166
* ['lang'] = (string) The script language (Required)

src/Elasticsearch/Endpoints/DeleteByQuery.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function setBody($body)
3636
* @throws \Elasticsearch\Common\Exceptions\RuntimeException
3737
* @return string
3838
*/
39-
protected function getURI()
39+
public function getURI()
4040
{
4141
if (!$this->index) {
4242
throw new Exceptions\RuntimeException(
@@ -55,7 +55,7 @@ protected function getURI()
5555
/**
5656
* @return string[]
5757
*/
58-
protected function getParamWhitelist()
58+
public function getParamWhitelist()
5959
{
6060
return array(
6161
'_source',
@@ -72,7 +72,7 @@ protected function getParamWhitelist()
7272
'ignore_unavailable',
7373
'lenient',
7474
'preference',
75-
'q',
75+
'query',
7676
'refresh',
7777
'request_cache',
7878
'requests_per_second',
@@ -96,7 +96,7 @@ protected function getParamWhitelist()
9696
/**
9797
* @return string
9898
*/
99-
protected function getMethod()
99+
public function getMethod()
100100
{
101101
return 'POST';
102102
}

src/Elasticsearch/Endpoints/Search.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ public function getParamWhitelist()
9393
'version',
9494
'fielddata_fields',
9595
'docvalue_fields',
96-
'filter_path'
96+
'filter_path',
97+
'terminate_after',
9798
);
9899
}
99100

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
namespace Elasticsearch\Endpoints;
4+
5+
use Elasticsearch\Common\Exceptions;
6+
7+
/**
8+
* Class UpdateByQuery
9+
*
10+
* @category Elasticsearch
11+
* @package Elasticsearch\Endpoints *
12+
* @author Zachary Tong <zachary.tong@elasticsearch.com>
13+
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
14+
* @link http://elasticsearch.org
15+
*/
16+
class UpdateByQuery 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+
if (is_array($body) !== true) {
31+
throw new Exceptions\InvalidArgumentException(
32+
'Body must be an array'
33+
);
34+
}
35+
$this->body = $body;
36+
37+
return $this;
38+
}
39+
40+
41+
/**
42+
* @throws \Elasticsearch\Common\Exceptions\BadMethodCallException
43+
* @return string
44+
*/
45+
public function getURI()
46+
{
47+
if (!$this->index) {
48+
throw new Exceptions\RuntimeException(
49+
'index is required for UpdateByQuery'
50+
);
51+
}
52+
53+
$uri = "/{$this->index}/_update_by_query";
54+
if ($this->type) {
55+
$uri = "/{$this->index}/{$this->type}/_update_by_query";
56+
}
57+
58+
return $uri;
59+
}
60+
61+
62+
/**
63+
* @return string[]
64+
*/
65+
public function getParamWhitelist()
66+
{
67+
return [
68+
'analyzer',
69+
'analyze_wildcard',
70+
'default_operator',
71+
'df',
72+
'explain',
73+
'fields',
74+
'fielddata_fields',
75+
'from',
76+
'ignore_unavailable',
77+
'allow_no_indices',
78+
'conflicts',
79+
'expand_wildcards',
80+
'lenient',
81+
'lowercase_expanded_terms',
82+
'preference',
83+
'q',
84+
'routing',
85+
'scroll',
86+
'search_type',
87+
'search_timeout',
88+
'size',
89+
'sort',
90+
'_source',
91+
'_source_exclude',
92+
'_source_include',
93+
'terminate_after',
94+
'stats',
95+
'suggest_field',
96+
'suggest_mode',
97+
'suggest_size',
98+
'suggest_text',
99+
'timeout',
100+
'track_scores',
101+
'version',
102+
'version_type',
103+
'request_cache',
104+
'refresh',
105+
'consistency',
106+
'scroll_size',
107+
'wait_for_completion',
108+
];
109+
}
110+
111+
112+
/**
113+
* @return string
114+
*/
115+
public function getMethod()
116+
{
117+
return 'POST';
118+
}
119+
}

0 commit comments

Comments
 (0)