Sitecore 9 Solr Highlight Search Results

Highlight Search Results

“Solr has a highlighting feature. This means that Solr returns fragment of the documents that match a query in the query response. Solr includes these fragments in a special section of the response that is called the highlighting section. Solr also includes formatting clues that you use to determine how you present these fragments (or snippets)” from documentation.

  • 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 description field on book template, which is used for highlighting search results.

Highlighter:

To show Highlight search result, created Highlighter content item, and added Highlighter 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 HighlightingParameters to pass field name, on which highlighting should run.
  • Passing query and queryoptions to IProviderSearchContext.Query() method.

Basic Layout code: (Basic.cshtml)

Highlighter component - Controller Rendering: (HighlighterController.cs)

View Model (HighlightResult.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

References:

Happy Learning :)

Share