| endpoint | mget |
|---|---|
| lang | javascript |
| es_version | 9.3 |
| client | @elastic/elasticsearch@9.3.0 |
Use client.mget() to retrieve multiple documents by ID in a single
request. This is significantly faster than issuing individual get
calls.
const response = await client.mget({
index: "products",
ids: ["prod-1", "prod-2", "prod-3"],
});
for (const doc of response.docs) {
if (doc.found) {
console.log(`${doc._id}: ${doc._source.name}`);
} else {
console.log(`${doc._id}: not found`);
}
}Fetch documents from different indices in a single call by specifying
_index per document:
const response = await client.mget({
docs: [
{ _index: "products", _id: "prod-1" },
{ _index: "orders", _id: "order-42" },
],
});Use _source to limit which fields are returned:
const response = await client.mget({
index: "products",
ids: ["prod-1", "prod-2"],
_source: ["name", "price"],
});