Skip to content

Latest commit

 

History

History
57 lines (47 loc) · 1.27 KB

File metadata and controls

57 lines (47 loc) · 1.27 KB
endpoint update
lang java
es_version 9.3
client co.elastic.clients:elasticsearch-java:9.3.0

Elasticsearch 9.3 update endpoint (Java example)

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.

Script updates

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

Upserts

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