Solr/Solrj: How can I determine the total number of documents in an index?

George Armhold picture George Armhold · Feb 19, 2011 · Viewed 29k times · Source

How can I determine the total number of documents in a Solr index using Solrj?

After hours of searching on my own, I actually have an answer (given below); I'm only posting this question so others can find the solution more easily.

Answer

George Armhold picture George Armhold · Feb 19, 2011

Here's what I'm using. Is this canonical? Is there a better way?

    SolrQuery q = new SolrQuery("*:*");
    q.setRows(0);  // don't actually request any data
    return server.query(q).getResults().getNumFound();