| endpoint | mget |
|---|---|
| lang | python |
| es_version | 9.3 |
| client | 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.
response = client.mget(
index="products",
ids=["prod-1", "prod-2", "prod-3"],
)
for doc in response["docs"]:
if doc["found"]:
print(f"{doc['_id']}: {doc['_source']['name']}")
else:
print(f"{doc['_id']}: not found")Fetch documents from different indices in a single call by specifying
_index per document:
response = client.mget(
docs=[
{"_index": "products", "_id": "prod-1"},
{"_index": "orders", "_id": "order-42"},
]
)Use _source to limit which fields are returned:
response = client.mget(
index="products",
ids=["prod-1", "prod-2"],
source_includes=["name", "price"],
)