Reputation: 171
I want to know the count of all the documetns present in an index,is it possible to get the count using java high level rest client COUNT API?
Upvotes: 1
Views: 3269
Reputation: 2415
You can get the count of all the documents in an index either using cat count or Count API. If you're using elasticsearch version 6.6 and above then you can follow this link to get the count using the Java High Level REST Client's Count API.
If you're using older versions then you have to use the Java Low Level REST Client to get the doc count.
As a RestHighLevelClient is built on top of Low Level REST Client.
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http")));
you can use this to get the low level client from RestHighLevelClient:
RestClient lowLevelClient = client.getLowLevelClient();
Perform the following command for elasticsearch version 6.3 and lower:
Response response = client.getLowLevelClient().performRequest("GET", indexName+"/_count");
Perform the following for elasticsearch version 6.3 till 6.5:
Request request = new Request("GET", indexName+"/_count");
client.getLowLevelClient().performRequest(request);
Convert the response into String:
String responseBody = EntityUtils.toString(response.getEntity());
Then you can parse the responseBody to get the count value.
Upvotes: 2