Total number of documents in pysolr

Viewed 2381

How can I get the total number of documents matching the given query. I have use the query below:

result = solr.search('ad_id : 20')
print(len(result))

Since the default returning value is '10', the output is only 10, but the count is 4000. How can I get the total number of counts?

5 Answers

As @MatsLindh mentioned -

result = solr.search('ad_id : 20')
print(result.hits)

Finally got the answer:

Added rows=1000000 at the end of the query.

result = solr.search('ad_id : 20', rows=1000000)

But if the rows are greater than this the number should be changed in the query. This might be a bad solution but works. If anyone has a better solution please do reply.

If you just want the total number of items that satisfy your query, here is my Python3 code (using the pysolr module):

    collection='bookindex'  # or whatever your collection is called
    solr_url = f"http://{SOLR_HOST}/solr/{collection}"
    solr = pysolr.Solr(url=solr_url, timeout=120, always_commit=True)
    result = solr.search("*:*", rows=0);
    return result.hits

This queries for all documents (":") -- 315913 in my case -- but you can narrow that to suit your requirements. For example, if I want to know how many of my book entries have title:pandas I can search("title:pandas", rows=0) and get 41 as the number that have pandas in the title. By setting rows=0 you're letting Solr know that it need not format any results for you but you just return the meta information, and thus much more efficient than setting a high limit on rows.

Related