| endpoint | update |
|---|---|
| lang | dotnet |
| es_version | 9.3 |
| client | Elastic.Clients.Elasticsearch==9.3.0 |
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.
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))
)
);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
))
);