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,
- Group Search Results
- Highlight Search Results
- Spell checker
- 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.
- 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.
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)
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 🙂