SolrNet

Monday, June 6, 2011

SolrNet Grouping example

Grouping and Pivoting support is now available for SolrNet.

Here is an example how to use the grouping feature within SolrNet.
To use it download the current source code of SolrNet from github. (select the master branch)
var url = "someurlwhereyoursolrisrunning";

Startup.Init<Model.doc>(url);

var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Model.doc>>();


var groupingParams = new GroupingParameters();

//Set the output groups, this means the output will have 2 groups
//one group for popularity and one for manu_exact
//these two groups are not nested
groupingParams.Fields = new[] { "popularity", "manu_exact" };

//Set the output format to grouped
groupingParams.Format = GroupingFormat.Grouped;

var queryOptions = new QueryOptions();

//Set count of how many groups you want to return
queryOptions.Rows = 20;

//Add the grouping parameters
queryOptions.Grouping = groupingParams;

//Set sorting of the groups on the field popularity
queryOptions.OrderBy = new[] { new SortOrder("popularity", Order.DESC) };

//Execute the query
var results = solr.Query("ipod", queryOptions);

//Examine the results
if (results.Grouping.Count > 0)
{
  foreach (var group in results.Grouping)
  {  
     Console.WriteLine(String.Format("Group {0}, has {1} subgroups", 
                     group.Key,
                     group.Value.Groups.Count.ToString()));
  }
}

1 comment:

Unknown said...

Hi,

Can you give an example how can i do a union?

Really appreaciate