| endpoint | update |
|---|---|
| lang | php |
| es_version | 9.3 |
| client | elasticsearch/elasticsearch==9.3.0 |
Use $client->update() to modify specific fields of an existing
document without replacing the entire document.
$response = $client->update([
'index' => 'products',
'id' => 'prod-1',
'body' => [
'doc' => ['price' => 799.99, 'in_stock' => false],
],
]);
echo "{$response['result']} document {$response['_id']}\n";Only the fields in doc are merged into the existing document. All
other fields remain unchanged.
Use a script for updates that depend on the current document state:
$response = $client->update([
'index' => 'products',
'id' => 'prod-1',
'body' => [
'script' => [
'source' => 'ctx._source.price *= params.discount',
'params' => ['discount' => 0.9],
],
],
]);Supply upsert to create the document if it does not already exist:
$response = $client->update([
'index' => 'products',
'id' => 'prod-3',
'body' => [
'doc' => ['price' => 599.00],
'upsert' => [
'name' => 'Ergonomic Standing Desk',
'brand' => 'DeskCraft',
'price' => 599.00,
'category' => 'furniture',
'in_stock' => true,
'rating' => 4.8,
],
],
]);