Obtain the elasticsearch client using RestClient for DELETE and SCROLLAPI


I'm using elasticsearch to save data coming from twitter.
At this point I'm having difficulty deleting data from index and using the Scroll API because after reading about ways to do that, everyone uses the Client and I'm using RestClient.

What I am using:

RestClient restClient = RestClient.builder(
 new HttpHost("localhost", 9200, "http"),
 new HttpHost("localhost", 9201, "http")).build();

What I found:

For Delete:

DeleteIndexResponse deleteResponse = client.admin().indices().delete(new DeleteIndexRequest("your-index")).actionGet()

For Scroll:

SearchResponse scrollResp = client.prepareSearch(test)
        .addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC)
        .setScroll(new TimeValue(60000))

How do I get the client using RestClient?
Will I have to create Java High Level REST Client?
Using the Transport Client it would be possible to do what I am looking for, but as it will be discontinued / deprecated, I used RestClient .Is it possible to use both?
Thanks and regards

(Luca Cavanna) #2

You can use the low-level REST client, but then you need to take care of marshalling the request body and unmarshalling the response body:

Response response = restClient.performRequest("DELETE", "/your-index");
HttpEntity entity = response.getEntity();
//read the response body from the entity

or you can use the high-level REST client, but keep in mind that as of today only the version that works against 6.0.0-beta1 was released. A version that works against 5.6 will be released in the coming weeks. If you are using a previous version of Elasticsearch, you cannot use the high-level REST client:

DeleteIndexResponse deleteResponse = client.delete(new DeleteIndexRequest("your-index"));

as you can see above, the high-level REST client code is very similar to the transport client example that you posted. Have a look at our migration page to know more: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.0/java-rest-high-level-migration.html .


Thanks for the answer. Is there a date for the transportClient to be
discontinued? greetings

(Luca Cavanna) #4

You are welcome. There isn't a date, but we are targeting version 7.0 for the removal and 6.0 GA for the deprecation most likely. There won't be any additions to it once it's deprecated.



Thanks for the answer.

Already, I would like to ask an issue, how can I in Java, check if a particular document already exists in elasticSearch? I have access to the index, type.

Basically, it is possible to do this:

curl -XHEAD 'http://localhost:9200/twitter/tweet/1

In java, but using the search for key: value or term?

I Tried:

Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("q", key+":"+value);
			paramMap.put("pretty", "true");	                                                               
			Response response = restClient.performRequest("GET", "/"+indexDir+"/tweet/_search/exists",paramMap);

But I could not.


(Luca Cavanna) #6

can be done with the low-level REST client as follows:

Response response = restClient.performRequest("HEAD", "/twitter/tweet/1");

(system) #7

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.