Using Solr to group search results
This topic is valid for Sitecore 9.0 and later.
Solr has a result grouping feature. You use this feature to sort documents into groups, based on a common field value. Solr returns the top documents for each group.
For example, if a user searches for "DVD", Solr can return three categories such as "TV and Video," "Movies," and "Computers," with three results per category. In this case, the query term "DVD" appears in all three categories. Solr groups the results together in order to be more useful for the end-users.
You do not have to configure anything in Solr to use this feature.
There is more information here:
https://cwiki.apache.org/confluence/display/solr/Result+Grouping
Use with Sitecore
Use with Sitecore
Sitecore has an API that you use to access the result grouping feature of Solr. The API is designed as an extension to the existing ContentSearch API.
Follow these steps to use the API in your project:
-
Reference the following DLL files into your project (you can get them from the
binfolder of your Sitecore website):-
SolrNet.dll -
Sitecore.ContentSearch.dll -
Sitecore.ContentSearch.SolrProvider.dll -
Sitecore.ContentSearch.SolrNetExtension.dll
-
-
Import the following namespace:
-
Sitecore.ContentSearch.SolrNetExtension(an extension of SolrNet that contains the implementation of the SuggesterComponent)
-
You can now use the Result Grouping API as part of ISearchIndex:
-
Create your query as string or any implementation of
ISolrQuery. -
Call
IProviderSearchContext.Query()method and inspect the returned result of typeSolrQueryResults<T>.
The returned result contains a Grouping property that is a Dictionary<string, GroupedResults<T>>, where Key is the group field name and Value is an object of GroupedResults that represents the count of unique matching documents that are grouped along with the list of grouped documents (ICollection<Group<T>>).
Each Group<T> object contains the Solr documents, group value, and the number of documents in the group.
For example:
API documentation
API documentation
Query
Executes the query against Solr.
Syntax
Parameters
-
Context
Type:
Sitecore.ContentSearch.IProviderSearchContextThe search provider context
-
qThe Solr query string
-
Options
Type:
SolrNet.Commands.Parameters.QueryOptionsQuery options
Type parameters
-
TThe type of the result item
Remarks
The method will execute the query (the first argument) against Solr with the defined set of options (the second parameter) and retrieve the result as a dictionary of values.
