You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by arian487 <ak...@tagged.com> on 2011/05/10 04:43:39 UTC

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.

RE: SolrQuery API for adding group filter

Posted by arian487 <ak...@tagged.com>.
I'm actually using php but I get what you're saying.  I think I understand
what I need to do.  Thanks a lot man!

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

RE: SolrQuery API for adding group filter

Posted by arian487 <ak...@tagged.com>.
I actually have another question unrelated to this (but related to grouping). 
I'm wondering if I can do a more complex grouping, such as grouping by a
field and also making sure it matches some other criteria (such as date). 
For example, currently it might group 5 items from some field, but the 5th
item for example is from a really far date which I don't want grouped with
these more recent items.  

Basically I want it to look like this:

Group1 all has common field 'x' and ALSO is items from today
Group2 all has common field 'x' again but now its items are from yesterday,
etc...

I'm having trouble figuring out how that'd work, any help would be
appreciated!

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

RE: SolrQuery API for adding group filter

Posted by "Dyer, James" <Ja...@ingrambook.com>.
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:akarbasi@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.