George Armhold
George Armhold

Reputation: 31104

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

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.

Upvotes: 46

Views: 40554

Answers (5)

jakebugz617
jakebugz617

Reputation: 349

solr numFound response

Depending how you are using it, notice the format options available. For my case XML is ideal.

http://localhost:8983/solr/drupal/select?q=*%3A*&rows=0&wt=xml

Upvotes: 2

steve
steve

Reputation: 11

Here's what I use to get total docs using JSON/PHP hope this helps

// Get total number of documents in solr
$solrObj = file_get_contents("http://HOSTNAME:8983/solr/COLLECTION 
NAME/select?q=*:*&rows=0&wt=json");
// var_dump(json_decode($solrObj));
$res_obj = json_decode($solrObj);
$numDocs = $res_obj->response->numFound; 
echo "Total number docs found!: ".$numDocs."<br />";

Upvotes: 1

michalrudko
michalrudko

Reputation: 1530

Pasting the whole curl:

curl -s --negotiate -u: 'hostname:8983/solr/my_collection/query?q=*:*&rows=0' | jq '.response | .numFound'
1868000278

Upvotes: 2

whomer
whomer

Reputation: 615

Your answer of sending the query *:* is probably the best, most general solution. Especially if you are using SolrCloud. However, there is an alternate solution, the Solr Core Admin API

Upvotes: 3

George Armhold
George Armhold

Reputation: 31104

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();

Upvotes: 57

Related Questions