Solr/Solrj: How can I determine the total number of documents in an index?
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
Related videos on Youtube
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, 2022Comments
-
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 almost 8 yearsFor a quick REST API check, something like:
http://hostname:8983/solr/collection_name_here/query?debug=query&q=*:*
andnumFound
contains the count. -
motagirl2 about 5 yearsSomething like
wget http://hostname:8983/solr/collection_name_here/query?q=*:*
also does the trick :)` (and stores the result in a file calledquery?q=*:*
) -
Jim Bob about 4 yearsAdd the rows parameter to return no data, so just the count in "numFound":
http://hostname:8983/solr/collection_name_here/query?q=*:*&rows=0