GrahamG21 GrahamG21 - 1 year ago 174
Java Question

Possible to get the size of an Elasticsearch index through Java API?

I have been trying to get the size of my Elasticsearch index through the Java API, and I have not been able to find the right call to accomplish this. I found some suggestions like this one (!topic/elasticsearch/jNCjCqAS1us), but it is from 2012 and seems to no longer be relevant.

I have been able to get an

the following way:

IndicesStatsResponse response = client.admin().indices()

but from this point I cannot find the information that I need. Is this possible?

Answer Source

The index stats can be accessed using the Stats API.

  • Using cURL: curl -XGET localhost:9200/index_name/_stats?pretty=true where under store you have size_in_bytes

  • Using the Java API: IndexStatsResponse is a response that has to be converted to a JSON if you want to read it. You can use gson to parse te Json.

        IndicesStatsResponse indicesStatsResponse = StartTCPService.getClient()
        XContentBuilder builder = XContentFactory.jsonBuilder();
        indicesStatsResponse.toXContent(builder, ToXContent.EMPTY_PARAMS);
        String jsonResponse = builder.prettyPrint().string();
        JsonParser jsonParser = new JsonParser(); // from import;
        Long sizeOfIndex = jsonParser.parse(jsonResponse)
        System.out.println(sizeOfIndex); // this is in Bytes
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download