| endpoint | get |
|---|---|
| lang | java |
| es_version | 9.3 |
| client | co.elastic.clients:elasticsearch-java:9.3.0 |
Use client.get() to retrieve a document by its ID, deserialised into
a typed class.
var response = client.get(g -> g
.index("products")
.id("prod-1"),
Product.class
);
if (response.found()) {
Product product = response.source();
System.out.println(product.name() + " — $" + product.price());
}The response includes metadata (index(), id(), version(),
seqNo(), primaryTerm()) alongside the typed source().
Use sourceIncludes or sourceExcludes to return only the fields you
need:
var response = client.get(g -> g
.index("products")
.id("prod-1")
.sourceIncludes("name", "price"),
Product.class
);Check response.found() rather than catching exceptions:
var response = client.get(g -> g
.index("products").id("prod-999"),
Product.class
);
if (!response.found()) {
System.out.println("Document not found");
}