Skip to content

Latest commit

 

History

History
53 lines (43 loc) · 1.05 KB

File metadata and controls

53 lines (43 loc) · 1.05 KB
endpoint mget
lang javascript
es_version 9.3
client @elastic/elasticsearch@9.3.0

Elasticsearch 9.3 mget endpoint (JavaScript example)

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`);
  }
}

Cross-index retrieval

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" },
  ],
});

Selecting fields

Use _source to limit which fields are returned:

const response = await client.mget({
  index: "products",
  ids: ["prod-1", "prod-2"],
  _source: ["name", "price"],
});