I recently set up a solrj application that uses Solr Trunk and grouping.  I 
didn't see where there was any explicit support in solrj for grouping (in 
Trunk...Maybe there is in the old SOLR-236 version).  But you can set any 
parameters on the request like this:

SolrQuery query = new SolrQuery("...");
query.set("group", true);
query.set("group.field", "FIELD_TO_GROUP_ON");

Getting the response back is the tricky part.  You have to parse the raw 
response because once again, I didn't see any explicit support.  Here's a 
simple example how to do it.  I did some quick editing on this and stripped out 
all of the null checks, etc for clarity.  You'll also need additional code if 
your "group.limit" is something other than 1...

QueryResponse resp = server.query( ... );
...
NamedList respNL = resp.getResponse();
NamedList groupInfo = (NamedList) respNL.get("grouped");
NamedList thisGroupInfo = (NamedList) groupInfo.get("FIELD_TO_GROUP_ON");
Number totalUngrouped = (Number) thisGroupInfo.get("matches");
long totalNumberOfUngroupedDocuments = totalUngrouped.longValue();
List<Object> groupData = (List<Object>) thisGroupInfo.get("groups");
int numberOfGroupsReturnedOnThisPage = groupData.size();
for(Object o : groupData) {
        NamedList thisGroup = (NamedList) o;
        SolrDocumentList sdl = (SolrDocumentList) thisGroup.get("doclist");
        long totalDocsInThisGroup = sdl.getNumFound();
        int totalDocsReturnedForThisGroup = sdl.size();
        SolrDocument groupedDoc = sdl.get(0);
        //do something with the document here...
}

As an alternative to getting the response back like this, I believe if you 
specify "group.format=simple" and "group.main=true", then the response will 
come back with a normal SolrDocumentList and you won't need code like above.  
One caveat to this is my testing showed a significant performance and/or memory 
usage hit from using these two parameters.  I didn't dig any deeper to try and 
figure out why.

James Dyer
E-Commerce Systems
Ingram Content Group
(615) 213-4311

-----Original Message-----
From: arian487 [mailto:akarb...@tagged.com] 
Sent: Monday, May 09, 2011 9:44 PM
To: solr-user@lucene.apache.org
Subject: SolrQuery API for adding group filter

There doesn't seem to be API to add a group (like group.field or group=true). 
I'm very new to this so I'm wondering how I'd go about adding a group query
much like how I use 'addFilterQuery' to add an fq.  Thanks.  

--
View this message in context: 
http://lucene.472066.n3.nabble.com/SolrQuery-API-for-adding-group-filter-tp2921539p2921539.html
Sent from the Solr - User mailing list archive at Nabble.com.

Reply via email to