Skip to content

Latest commit

 

History

History
39 lines (30 loc) · 864 Bytes

File metadata and controls

39 lines (30 loc) · 864 Bytes
endpoint exists
lang java
es_version 9.3
client co.elastic.clients:elasticsearch-java:9.3.0

Elasticsearch 9.3 exists endpoint (Java example)

Use client.exists() to check whether a document exists without fetching its contents.

var response = client.exists(e -> e
    .index("products")
    .id("prod-1")
);

if (response.value()) {
    System.out.println("Document exists");
} else {
    System.out.println("Document not found");
}

This issues a HEAD request, so no document body is transferred.

Conditional logic

Use exists to guard operations that depend on prior state:

var exists = client.exists(e -> e.index("products").id("prod-1"));
if (!exists.value()) {
    client.index(i -> i.index("products").id("prod-1").document(product));
}

For atomic create-if-not-exists, prefer client.create() instead.