Skip to content

Latest commit

 

History

History
56 lines (44 loc) · 1.1 KB

File metadata and controls

56 lines (44 loc) · 1.1 KB
endpoint get
lang java
es_version 9.3
client co.elastic.clients:elasticsearch-java:9.3.0

Elasticsearch 9.3 get endpoint (Java example)

Use client.get() to retrieve a document by its ID, deserialised into a typed class.

var response = client.get(g -> g
    .index("products")
    .id("prod-1"),
    Product.class
);

if (response.found()) {
    Product product = response.source();
    System.out.println(product.name() + " — $" + product.price());
}

The response includes metadata (index(), id(), version(), seqNo(), primaryTerm()) alongside the typed source().

Selecting fields

Use sourceIncludes or sourceExcludes to return only the fields you need:

var response = client.get(g -> g
    .index("products")
    .id("prod-1")
    .sourceIncludes("name", "price"),
    Product.class
);

Handling missing documents

Check response.found() rather than catching exceptions:

var response = client.get(g -> g
    .index("products").id("prod-999"),
    Product.class
);

if (!response.found()) {
    System.out.println("Document not found");
}