ElasticSearch: Low Level Client Get

This entry is part 3 of 3 in the series ElasticSearch Low Level Rest Client
(Last Updated On: )

In this tutorial I will show you how to put a json document into ElasticSearch. If you have not first connected to ElasticSearch please do so before continuing.

POM.xml

  1. <dependency>
  2. <groupId>com.fasterxml.jackson.core</groupId>
  3. <artifactId>jackson-databind</artifactId>
  4. <version>2.9.5</version>
  5. </dependency>

Imports

  1. import org.apache.http.HttpEntity;
  2. import org.apache.http.nio.entity.NStringEntity;
  3. import org.apache.http.entity.ContentType;
  4. import org.elasticsearch.client.Response;
  5. import org.apache.http.util.EntityUtils;

Now perform the GET request using the low level client.

  1. ObjectMapper objectMapper = new ObjectMapper();
  2. final String document = "{\"key\": 1 }";
  3. final JsonNode document = objectMapper.readTree("{" +
  4. " \"query\": {" +
  5. " \"match\" : {" +
  6. " \"key\" : 1 }}}");
  7. final HttpEntity httpEntity = new NStringEntity(document.toString(), ContentType.APPLICATION_JSON);
  8. final Response response = restClient.performRequest("GET", "/indexName/indexType/_search", Collections.<String, String>emptyMap(), httpEntity);
  9. //Now you can print the response
  10. System.out.println(EntityUtils.toString(response.getEntity()));
  11.  
  12. //OR get the content
  13. final JsonNode content = objectMapper.readTree(response.getEntity().getContent());
  14. System.out.println(content);
Series Navigation<< ElasticSearch: Low Level Client Put