ElasticSearch: Low Level Client Put

This entry is part 2 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.

Imports

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

Now perform the PUT request using the low level client.

  1. final String document = "{\"key\": 1 }";
  2. final HttpEntity httpEntity = new NStringEntity(document, ContentType.APPLICATION_JSON);
  3. final Integer id = 1;
  4. final Response response = restClient.performRequest("PUT", "/indexName/indexType/" + id, Collections.<String, String>emptyMap(), httpEntity);
  5.  
  6. //Now you can print the response
  7. System.out.println(EntityUtils.toString(response.getEntity()));
Series Navigation<< ElasticSearch: Low Level Rest Client ConnectionElasticSearch: Low Level Client Get >>