ElasticSearch: High Level Client Post

This entry is part 2 of 4 in the series ElasticSearch High Level Rest Client
(Last Updated On: )

In this tutorial I will show you how to perform a POST request. If you have not connected first please do so before continuing.

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.apache.http.util.EntityUtils;
  5. import org.elasticsearch.client.Response;

Now we can perform the POST to ElasticSearch.

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