Search code examples
solrsolrnetfieldcollapsing

Solr/SolrNet Grouping results


I'm using Solr for a product catalogue built on asp.net with solrnet and everything works pretty well so far. I need to extend the search results by adding some grouping option to it.

First I’ll explain how the schema looks like:

 <field name="product_id" type="string" indexed="true" stored="true" required="true" /> 
 <field name="name" type="string" indexed="true" stored="true"/>
 <field name="merchant" type="string" indexed="true" stored="true"/>
 <field name="merchant_id" type="int" indexed="false" stored="true"/>
 <field name="merchant_logo" type="string" indexed="false" stored="true"/>
 <field name="brand" type="string" indexed="true" stored="true"/>
 <field name="brand_id" type="int" indexed="false" stored="true"/>
 <field name="group_id" type="int" indexed="true" stored="true"/>
 <field name="group_name" type="string" indexed="true" stored="true"/>
 <field name="has_group" type="boolean" indexed="true" stored="true"/>

So as you see products above has a “group_id” and “group name” also I have Boolean flag named “has group” on my schema.

Here are some examples of products results

Product A – group id 1 - group Name 1 – has group true
Product B – group id 1 - group Name 1 – has group true
Product C – group id 2 - group Name 2 – has group true
Product D – group id 2 - group Name 2 – has group true
Product E – has group false
Product F – has group false

But when I’m showing these results, I need to show the products with group names (or ids) on top the results by Its group name. And the products which doesn’t belongs to a group will be listed on right after the groups.

So my results should looks like:

Group Name 1
Group Name 2
Product E
Product F

Indeed I need to implement the pagination as well, so in other words when someone search for some product if a the products contains group names I’ll list them on top of the results and next the other products.

Is this even possible? If it is possible, what would I need to do. I already read about FieldCollapsing in solr but still I have no idea whether this is the right way or not http://wiki.apache.org/solr/FieldCollapsing


Solution

  • Yes, if you want grouping over the whole index you need the field collapsing feature. SolrNet's support for this is currently broken, since it's an unreleased feature in Solr and changed a lot since the first implementation in SolrNet. See http://code.google.com/p/solrnet/issues/detail?id=127 for details and updates.