How to count records using JAVA client api?


(Suman Santra) #1

Hi,

I am using following code to get count of records indexed which satisfy the given criteria, but this is not working and giving me all the records indexed in the type. How do I get count of records satisfying the criteria.

SearchRequestBuilder searchRequest = client.prepareSearch(indexName);
searchRequest.setTypes(typeName);
searchRequest.setRouting(searchReq.getRouting());
SearchResponse response = searchRequest.setSource(new SearchSourceBuilder().size(10000).query(QueryBuilders.queryStringQuery(queryString).get();

return response.getHits().getHits().length;

The above code is not working in Elasticsearch 5 Java client api, please help.


(Adrien Grand) #2

Something like that should work:

return client().prepareSearch(indexName)
    .setTypes(typeName)
    .setSize(0)
    .setQuery(QueryBuilders.queryStringQuery(queryString)).get().getHits().getTotalHits();

(system) #3

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