0

In a java application I want to find out, whether my index is empty or not (containing zero documents, or containing at least one documents).

I have the feeling, that counting all documents (by sending a search request with size=0) is not readable and might not be optimal in performance.

Is there a dedicated way for to check, whether an index is empty?

1 Answer 1

1

You could do that with the following Java code:

IndicesStatsResponse indicesStatsResponse = client().admin().indices().prepareStats(INDEX).get();

indicesStatsResponse.getIndices().get(INDEX).getTotal().docs.getCount());

where client is org.elasticsearch.client.Client and INDEX is the String, representing the name of the index.

Interesting is, that some simple test shows, that in average, this request is faster by 20%, than doing MatchAllDocsQuery. But I'm not sure if this is a correct test. Maybe I will do a proper one later

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.