| endpoint | update_by_query |
|---|---|
| lang | php |
| es_version | 9.3 |
| client | elasticsearch/elasticsearch==9.3.0 |
Use $client->updateByQuery() to update all documents matching a
query using a script.
$response = $client->updateByQuery([
'index' => 'products',
'body' => [
'query' => [
'term' => ['category' => 'electronics'],
],
'script' => [
'source' => 'ctx._source.price *= params.discount',
'params' => ['discount' => 0.9],
],
],
]);
echo "Updated {$response['updated']} documents\n";By default, version conflicts abort the operation. Set
conflicts to proceed to skip conflicting documents and continue:
$response = $client->updateByQuery([
'index' => 'products',
'conflicts' => 'proceed',
'body' => [
'query' => ['term' => ['in_stock' => true]],
'script' => ['source' => 'ctx._source.in_stock = false'],
],
]);Use max_docs to cap the number of updates, and scroll_size to
control batch size:
$response = $client->updateByQuery([
'index' => 'products',
'max_docs' => 500,
'scroll_size' => 100,
'body' => [
'query' => ['match_all' => new \stdClass()],
'script' => ['source' => 'ctx._source.rating += 0.1'],
],
]);