Elasticsearch bulk insert using rest client Elasticsearch bulk insert using rest client elasticsearch elasticsearch

Elasticsearch bulk insert using rest client


Yes, that's correct, for now the REST client only allows to send raw REST queries to ES but nothing too sophisticated. Elastic is working on a high-level client next that will work on top of the REST client and allow you to send DSL queries, etc.

For now, here is a sample code that you can use to send documents in bulk to your ES server:

RestClient client = ...;String actionMetaData = String.format("{ \"index\" : { \"_index\" : \"%s\", \"_type\" : \"%s\" } }%n", index, type);List<String> bulkData = ...; // a list of your documents in JSON strings    StringBuilder bulkRequestBody = new StringBuilder();for (String bulkItem : bulkData) {    bulkRequestBody.append(actionMetaData);    bulkRequestBody.append(bulkItem);    bulkRequestBody.append("\n");}HttpEntity entity = new NStringEntity(bulkRequestBody.toString(), ContentType.APPLICATION_JSON);try {    Response response = client.performRequest("POST", "/your_index/your_type/_bulk", Collections.emptyMap(), entity);    return response.getStatusLine().getStatusCode() == HttpStatus.SC_OK;} catch (Exception e) {    // do something}


Another example in addition to Val answer:http://web.archive.org/web/20180813044955/http://cscengineer.net/2016/10/22/elastic-search-bulk-api/

Just use POST instead of PUT (be careful with the .exchange in the use of rest template)