45

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.

George Armhold
  • 30,824
  • 50
  • 153
  • 232

5 Answers5

55

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();
George Armhold
  • 30,824
  • 50
  • 153
  • 232
  • 13
    For a quick REST API check, something like: `http://hostname:8983/solr/collection_name_here/query?debug=query&q=*:*` and `numFound` contains the count. – cfeduke Jun 23 '16 at 21:24
  • Something like `wget http://hostname:8983/solr/collection_name_here/query?q=*:*` also does the trick :)` (and stores the result in a file called `query?q=*:*`) – motagirl2 Apr 25 '19 at 19:07
  • 6
    Add the rows parameter to return no data, so just the count in "numFound": `http://hostname:8983/solr/collection_name_here/query?q=*:*&rows=0` – Jim Bob Mar 27 '20 at 20:57
2

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

mlissner
  • 17,359
  • 18
  • 106
  • 169
whomer
  • 575
  • 9
  • 21
1

Pasting the whole curl:

curl -s --negotiate -u: 'hostname:8983/solr/my_collection/query?q=*:*&rows=0' | jq '.response | .numFound'
1868000278
michalrudko
  • 1,432
  • 2
  • 16
  • 30
0

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 />";
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
steve
  • 1
  • 1
0

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
jakebugz617
  • 319
  • 1
  • 11