Skip to content

Latest commit

 

History

History
64 lines (54 loc) · 1.4 KB

File metadata and controls

64 lines (54 loc) · 1.4 KB
endpoint update
lang php
es_version 9.3
client elasticsearch/elasticsearch==9.3.0

Elasticsearch 9.3 update endpoint (PHP example)

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.

Script updates

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],
        ],
    ],
]);

Upserts

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,
        ],
    ],
]);