nufar
nufar

Reputation: 77

How to extract document id from response - ElasticSearch Java Low Level Rest Client

I'm indexing document without id into ElasticSearch using the following code:

Response response = restClient.performRequest(
            HttpPost.METHOD_NAME,
            "/posts/doc/",
            Collections.emptyMap(),
            entity);

I want to extract the document id that was generated by ElasticSearch from the response. Is there any way to do that?

Upvotes: 0

Views: 549

Answers (2)

Narendra Prasath
Narendra Prasath

Reputation: 1531

response.getEntity().getContent() is InputStream object.

So, you could you BufferedReader also.

Try this:

BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuilder out = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
    out.append(line);
}
System.out.println(out.toString());

Upvotes: 0

dadoonet
dadoonet

Reputation: 14492

You need to read the response object for that response.getEntity().getContent().

If you are using Jackson, you can then deserialize the stream as a Map mapper.readValue(response.getEntity().getContent(), new TypeReference<Map<String, Object>>(){});

Then from the map, read the _id field.

Hope this helps.

Upvotes: 1

Related Questions