Get document with Id in OpenSearch via Java client

Learn how to get or search a document with a given id in an index in OpenSearch via Java client.

OpenSearch provides a Java SDK that makes it easier to communicate with OpenSearch through any Java application.

With a simple get request that is available on the OpenSearch client, we can get a document with the given id from the specified index.

GetResponse<Object> response = openSearchClient.get(g -> {
   g.index("sample-index").id("sample-id");
   return g;
}, Object.class);

response.source(); // to the get the data