| endpoint | update |
|---|---|
| lang | java |
| es_version | 9.3 |
| client | co.elastic.clients:elasticsearch-java:9.3.0 |
Use client.update() to modify specific fields of an existing document
without replacing the entire document.
var response = client.update(u -> u
.index("products")
.id("prod-1")
.doc(Map.of("price", 799.99, "in_stock", false)),
Product.class
);
System.out.println(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 = client.update(u -> u
.index("products")
.id("prod-1")
.script(s -> s
.source(src -> src.scriptString("ctx._source.price *= params.discount"))
.params("discount", JsonData.of(0.9))
),
Product.class
);Supply upsert to create the document if it does not already exist:
var response = client.update(u -> u
.index("products")
.id("prod-3")
.doc(Map.of("price", 599.00))
.upsert(new Product(
"Ergonomic Standing Desk", "DeskCraft",
599.00, "furniture", true, 4.8
)),
Product.class
);