| endpoint | mget |
|---|---|
| lang | java |
| es_version | 9.3 |
| client | co.elastic.clients:elasticsearch-java: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.
public record Product(String name, String brand, double price,
String category,
@JsonProperty("in_stock") boolean inStock,
double rating) {}
var response = client.mget(m -> m
.index("products")
.ids(List.of("prod-1", "prod-2", "prod-3")),
Product.class
);
for (var doc : response.docs()) {
if (doc.result().found()) {
var product = doc.result().source();
System.out.println(doc.result().id() + ": " + product.name());
} else {
System.out.println(doc.result().id() + ": not found");
}
}Fetch documents from different indices in a single call by specifying the index per document:
var response = client.mget(m -> m
.docs(
d -> d.index("products").id("prod-1"),
d -> d.index("orders").id("order-42")
),
JsonData.class
);Use sourceIncludes to limit which fields are returned:
var response = client.mget(m -> m
.index("products")
.ids(List.of("prod-1", "prod-2"))
.sourceIncludes("name", "price"),
Product.class
);