Solr: how to get the total number of results for a batch query using the Java API

I have the following:

43 documents indexed in Solr

If I use the Java API to execute a request without any grouping, for example:

SolrQuery query = new SolrQuery("*:*"); query.setRows(10); 

Then I can get the total number of matching elements:

 solrServer.query(query).getResults().getNumFound(); //43 in this case 

The getResults() method returns an instance of SolrDocumentList that contains this value.

If, however, I use grouping, for example:

 query.set("group", "true"); query.set("group.format", "simple"); query.set("group.field", "someField"); 

Then the above code to get the query results does not work loger (throws NPE), and instead I have to use:

 List<GroupCommand> groupCommands = solrServer.query(query).getGroupResponse().getValues(); List<Group> groups = groupCommand.getValues(); Group group = groups.get(groupIndex); 

I don’t understand how to use this part of the API to get the total number of relevant documents (43 from the non-group request above). At first I thought it was already impossible to get this with grouping, but I noticed that if I make a similar request in the Solr admin console, with the same grouping and everything, it will return the same results as the Java API and also numFound=43 . Thus, it is obvious that the code used for the console has some way to get this value even when using grouping:

enter image description here

My question is, how can I get this total number of matching documents for a query using grouping done through the Solr Java API?

+4
source share
2 answers

When viewing the Group source that is returned from your call to groups.get(groupIndex) , it has a getResults () method that returns a SolrDocumentList . SolrDocumentList has a getNumFound () method that should return a total, I believe ...

So you should be able to get the following:

  int numFound = group.getResults().getNumFound(); 

Hope this helps.

Update: I believe that, as OP pointed out, group.getResults (). getNumFound () returns only the number of elements in the group. However, there is a getMatches () method on GroupCommand that can match the corresponding account.

  int matches = groupCommands.getMatches(); 
+6
source

If the ngroups parameter is set to true (false by default), this will return the number of groups.

eg:

solrQuery.set("group.ngroups", true);

https://cwiki.apache.org/confluence/display/solr/Result+Grouping

this can be obtained from your responding GroupCommand with:

int numGroups = tempGroup.getNGroups();

At least that was my understanding?

+3
source

All Articles