| endpoint | get |
|---|---|
| lang | javascript |
| es_version | 9.3 |
| client | @elastic/elasticsearch@9.3.0 |
Use client.get() to retrieve a document by its ID.
const response = await client.get({ index: "products", id: "prod-1" });
const doc = response._source;
console.log(`${doc.name} — $${doc.price}`);The response includes metadata (_index, _id, _version,
_seq_no, _primary_term) alongside the _source document body.
Use _source_includes or _source_excludes to return only the fields
you need:
const response = await client.get({
index: "products",
id: "prod-1",
_source_includes: ["name", "price"],
});To fetch metadata without the source body, disable it entirely:
const response = await client.get({
index: "products",
id: "prod-1",
_source: false,
});A ResponseError with status 404 is thrown when the document does not
exist:
try {
await client.get({ index: "products", id: "prod-999" });
} catch (err) {
if (err.statusCode === 404) {
console.log("Document not found");
} else {
throw err;
}
}