| endpoint | get_source |
|---|---|
| lang | java |
| es_version | 9.3 |
| client | co.elastic.clients:elasticsearch-java:9.3.0 |
Use client.getSource() to retrieve only the document body, without
metadata, deserialised into a typed class.
var response = client.getSource(g -> g
.index("products")
.id("prod-1"),
Product.class
);
System.out.println(response.name() + " — $" + response.price());Use sourceIncludes or sourceExcludes to return a subset of fields:
var response = client.getSource(g -> g
.index("products")
.id("prod-1")
.sourceIncludes("name", "price"),
Product.class
);An ElasticsearchException with status 404 is thrown when the document
does not exist:
try {
client.getSource(g -> g.index("products").id("prod-999"), Product.class);
} catch (ElasticsearchException e) {
if (e.status() == 404) {
System.out.println("Document not found");
} else {
throw e;
}
}