| endpoint | get_source |
|---|---|
| lang | javascript |
| es_version | 9.3 |
| client | @elastic/elasticsearch@9.3.0 |
Use client.getSource() to retrieve only the document body, without
metadata. This is a convenience over get() when you don't need
_version, _seq_no, or other metadata fields.
const doc = await client.getSource({ index: "products", id: "prod-1" });
console.log(`${doc.name} — $${doc.price}`);Use _source_includes or _source_excludes to return a subset of
fields:
const doc = await client.getSource({
index: "products",
id: "prod-1",
_source_includes: ["name", "price"],
});A ResponseError with status 404 is thrown when the document does not
exist:
try {
await client.getSource({ index: "products", id: "prod-999" });
} catch (err) {
if (err.statusCode === 404) {
console.log("Document not found");
} else {
throw err;
}
}