- ElasticSearch: Low Level Rest Client Connection
- ElasticSearch: Low Level Client Put
- ElasticSearch: Low Level Client Get
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
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- <version>2.9.5</version>
- </dependency>
Imports
- import org.apache.http.HttpEntity;
- import org.apache.http.nio.entity.NStringEntity;
- import org.apache.http.entity.ContentType;
- import org.elasticsearch.client.Response;
- import org.apache.http.util.EntityUtils;
Now perform the GET request using the low level client.
- ObjectMapper objectMapper = new ObjectMapper();
- final String document = "{\"key\": 1 }";
- final JsonNode document = objectMapper.readTree("{" +
- " \"query\": {" +
- " \"match\" : {" +
- " \"key\" : 1 }}}");
- final HttpEntity httpEntity = new NStringEntity(document.toString(), ContentType.APPLICATION_JSON);
- final Response response = restClient.performRequest("GET", "/indexName/indexType/_search", Collections.<String, String>emptyMap(), httpEntity);
- //Now you can print the response
- System.out.println(EntityUtils.toString(response.getEntity()));
- //OR get the content
- final JsonNode content = objectMapper.readTree(response.getEntity().getContent());
- System.out.println(content);