Skip to content

Latest commit

 

History

History
62 lines (51 loc) · 1.46 KB

File metadata and controls

62 lines (51 loc) · 1.46 KB
endpoint mget
lang java
es_version 9.3
client co.elastic.clients:elasticsearch-java:9.3.0

Elasticsearch 9.3 mget endpoint (Java example)

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

Cross-index retrieval

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

Selecting fields

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