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

5

46

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.

Katheykathi answered 19/2, 2011 at 12:23 Comment(0)
K
57

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();
Katheykathi answered 19/2, 2011 at 12:24 Comment(3)
For a quick REST API check, something like: http://hostname:8983/solr/collection_name_here/query?debug=query&q=*:* and numFound contains the count.Rajiv
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=*:*)Sclaff
Add the rows parameter to return no data, so just the count in "numFound": http://hostname:8983/solr/collection_name_here/query?q=*:*&rows=0Communalism
I
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

Innocency answered 6/2, 2017 at 19:10 Comment(0)
O
2

Pasting the whole curl:

curl -s --negotiate -u: 'hostname:8983/solr/my_collection/query?q=*:*&rows=0' | jq '.response | .numFound'
1868000278
Otis answered 12/2, 2021 at 17:25 Comment(0)
G
1

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 />";
Gotthard answered 23/4, 2022 at 9:8 Comment(0)
P
1

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
Patentee answered 7/7, 2022 at 20:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.