Skip to content

Latest commit

 

History

History
52 lines (42 loc) · 1.25 KB

File metadata and controls

52 lines (42 loc) · 1.25 KB
endpoint update
lang dotnet
es_version 9.3
client Elastic.Clients.Elasticsearch==9.3.0

Elasticsearch 9.3 update endpoint (.NET example)

Use client.UpdateAsync() to modify specific fields of an existing document without replacing the entire document.

var response = await client.UpdateAsync<Product, object>(
    "products", "prod-1",
    u => u.Doc(new { Price = 799.99, InStock = false })
);
Console.WriteLine($"{response.Result} document {response.Id}");

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:

var response = await client.UpdateAsync<Product, object>(
    "products", "prod-1",
    u => u.Script(s => s
        .Source("ctx._source.price *= params.discount")
        .Params(p => p.Add("discount", 0.9))
    )
);

Upserts

Supply Upsert to create the document if it does not already exist:

var response = await client.UpdateAsync<Product, object>(
    "products", "prod-3",
    u => u
        .Doc(new { Price = 599.00 })
        .Upsert(new Product(
            "Ergonomic Standing Desk", "DeskCraft",
            599.00, "furniture", true, 4.8
        ))
);