| endpoint | exists |
|---|---|
| lang | java |
| es_version | 9.3 |
| client | co.elastic.clients:elasticsearch-java:9.3.0 |
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.
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.