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

33,596

Solution 1

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

Solution 2

Pasting the whole curl:

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

Solution 3

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

Share:
33,596

Related videos on Youtube

George Armhold
Author by

George Armhold

I'm a consultant, currently focusing on the following topics: Go! (Golang) Ruby, Ruby on Rails Docker and containers more generally search (ElasticSearch, Apache Solr & Bleve) I've worked on lots of other stuff in the past, including: Java Android and iOS apps Please visit my website for details.

Updated on April 24, 2022

Comments

  • George Armhold
    George Armhold about 2 years

    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.

  • cfeduke
    cfeduke almost 8 years
    For a quick REST API check, something like: http://hostname:8983/solr/collection_name_here/query?debug=q‌​uery&q=*:* and numFound contains the count.
  • motagirl2
    motagirl2 about 5 years
    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=*:*)
  • Jim Bob
    Jim Bob about 4 years
    Add the rows parameter to return no data, so just the count in "numFound": http://hostname:8983/solr/collection_name_here/query?q=*:*&r‌​ows=0