Sitecore 9 Solr Group Search Results

Sitecore 9 introduced lot of new features with respect to Solr. As a developer, main change we can notice first is, in local environment also we need to enable SSL for Solr service. As it is required by xConnect. Other than this, Sitecore 9 also included couple of Solr search improvements. Some of them are as below,

  1. Group Search Results
  2. Highlight Search Results
  3. Spell checker
  4. Auto-suggest
  5. Spatial Search

I must say, Sitecore documentation is very good in explaining all the above features.

In this post, let’s explore, how to achieve Group search results.

Group Search Results
Have you ever used Group By clause in Sql queries, where it returns single row for every grouped item. Solr Group results also works in the similar manner, except that it also returns top documents for each group.

we use this feature to sort documents into groups, based on a common field value.

  • Environment: Sitecore 9 update 1, and Solr 6.6.2.
  • References: As this is Solr specific feature, we need to refer following assemblies from our project.
    • SolrNet.dll
    • Sitecore.ContentSearch.dll
    • Sitecore.ContentSearch.SolrProvider.dll
    • Sitecore.ContentSearch.SolrNetExtension.dll
  • Solr Configuration: No configuration is required to enable this feature.

For demo purpose, i have created a book template, and added couple of book items. I have a type field on book template, which can have self-help, fiction, biography or non-fiction values, will be using this field for grouping search results.

Group:

To show Group search result, created GroupDemo content item, and added Group component (Controller Rendering) from the presentation details. (to main placeholder)

Following points, you can observe in the below code,

  • Passing search text through query string.
  • Using GroupingParameters to pass field name, on which grouping should run.
  • Passing query and queryoptions to IProviderSearchContext.Query() method.

Basic Layout code: (Basic.cshtml)

Group component - Controller Rendering: (GroupController.cs)

View (Index.cshtml)

Result:

Whole source code, with sitecore items is present at https://github.com/gopigujjula/Sitecore9SolrDemo

Note: this code is only demo purposes, you should not be using SolrQueryResults as model type in your view.

Happy Learning :)

Share