1

Before am indexing the documents i want to check whether my index is empty or not. Which means, i just want to get the count of all the docments. If it returns 0 i want to proceed some action else i want to proceed some other action.

Am using Elastic Search 6.2.3 version with RestHighLevelClient.

1 Answer 1

3

Just run search query with size equal 0. Then you will have access to total number of hits, which means how many documents are in the index.

SearchRequest searchRequest = new SearchRequest("index_name"); 
SearchSourceBuilder searchBuilder = new SearchSourceBuilder(); 
searchBuilder.query(QueryBuilders.matchAllQuery()); 
searchBuilder.size(0);
searchRequest.source(searchBuilder); 

SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchResponse.getHits();
long totalHits = hits.getTotalHits();
if(totalHits == 0) {
    // proceed
}
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.