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)

@using Sitecore.Mvc
<!doctype html>
<html>
<head>
<title>Sitecore Demo</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div>
<div>
@Html.Sitecore().Placeholder("top")
</div>
<div>
@Html.Sitecore().Placeholder("main")
</div>
<div>
@Html.Sitecore().Placeholder("bottom")
</div>
</div>
</body>
</html>
view raw basic.cshtml hosted with ❤ by GitHub

Group component – Controller Rendering: (GroupController.cs)

using Sitecore.ContentSearch;
using Sitecore.ContentSearch.SearchTypes;
using Sitecore.ContentSearch.SolrProvider.SolrNetIntegration;
using SolrNet;
using SolrNet.Commands.Parameters;
using System.Web.Mvc;
namespace SC9SolrDemo.Controllers
{
public class GroupController : Controller
{
public ActionResult Index()
{
string searchText = Request.QueryString["text"] ?? string.Empty;
if (string.IsNullOrEmpty(searchText))
return View();
var queryOptions = new QueryOptions
{
Grouping = new GroupingParameters
{
Fields = new[] { "type_t" },
Limit = 10
}
};
var indexName = $"sitecore_{Sitecore.Context.Database.Name}_index";
var index = ContentSearchManager.GetIndex(indexName);
SolrQueryResults<SearchResultItem> results;
using (var context = index.CreateSearchContext())
{
results = context.Query<SearchResultItem>($"(language_t:{searchText})", queryOptions);
}
return View(results);
}
}
}

View (Index.cshtml)

@using SolrNet;
@using Sitecore.ContentSearch.SearchTypes
@model SolrQueryResults<SearchResultItem>
@if (Model != null)
{
foreach (var group in Model.Grouping)
{
foreach (var grp in group.Value.Groups)
{
<div class="group">
<div class="group-header">
@grp.GroupValue
</div>
<div class="group-item">
@foreach (var item in grp.Documents)
{
<span>@item["Name"] @item["Language"]</span>
}
</div>
</div>
}
}
}
view raw Index.cshtml hosted with ❤ by GitHub

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 🙂