Skip to content

Latest commit

 

History

History
51 lines (41 loc) · 1.05 KB

File metadata and controls

51 lines (41 loc) · 1.05 KB
endpoint get_source
lang java
es_version 9.3
client co.elastic.clients:elasticsearch-java:9.3.0

Elasticsearch 9.3 get_source endpoint (Java example)

Use client.getSource() to retrieve only the document body, without metadata, deserialised into a typed class.

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

System.out.println(response.name() + " — $" + response.price());

Selecting fields

Use sourceIncludes or sourceExcludes to return a subset of fields:

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

Handling missing documents

An ElasticsearchException with status 404 is thrown when the document does not exist:

try {
    client.getSource(g -> g.index("products").id("prod-999"), Product.class);
} catch (ElasticsearchException e) {
    if (e.status() == 404) {
        System.out.println("Document not found");
    } else {
        throw e;
    }
}