How To

How to Highlight Search Terms Using ContentSearch in Sitecore With Solr

The following article provides detailed steps on how to enable search term highlighting when using Sitecore with Solr search engine. For the purpose of this article we will be using Sitecore 7.2 rev. 140256, the latest revision, as of the time of this writing.

In one of the previous articles I covered highlighting search terms using ContentSearch with Lucene, and it was a matter of time until we got the same requirements for Solr as well. ContentSearch provider is still being expanded, although it provides a good amount of the most commonly use functionality, there are some things that have not yet been accounted for. Unfortunately, one of these features turned out to be search term highlighting, and fortunately, we have a solution.

We were approaching the end of the development cycle when we got the last batch of requirements, which included website search. Right away, I noticed that search highlighting was on the list. Remembering that the same functionality was not available for Lucene, I didn’t expect it to exist for the less mature Solr provider. It turned out I was correct – after spending some time crawling through the ContentSearch API, I was not able to find anything that could be of use.

Just like with the solution for Lucene, it seemed natural to extend the IQueriable with highlighting functionality. The questions were – where and how. After working with the Sitecore Support team we have come up with a solution.

The Demo Highlight Repository provides a project that can be either compiled into a separate library, or used as a demo for creating your own search extensions for term highlighting. Should you decide to compile the project as is, I would recommend to update the namespace naming to something more meaningful. Please feel free to clone the repository branch out an push your own enhancements (could this be the beginning for the ultimate IQueriable extension library?).

Once the code has been compiled, the functionality can be used in the following manner:


using Sitecore.HighlightDemo.Solr; 
...
using (var context = index.CreateSearchContext(SearchSecurityOptions.DisableSecurityCheck))
{
     var result = context.<strong>GetExtendedQueryable</strong><SearchResultItem>().Where(it => it["definition"] == "panda").<strong>GetResultsWithHighlights</strong>("definition");
     var highlights = result.Highlights;
}

 

The GetResultsWithHighlights also accepts an array of strings as a parameter, allowing to get highlights for multiple fields.

After execution of the Linq query, you should get the similar Solr native query in the Search log files:

?q=definition_t:(panda)&rows=500&fl=*,score&fq=_indexname:(sitecore_master_index)&hl=true&hl.fl=definition_t

 

Voila! The search terms are now being highlighted! Many thanks to Sitecore Support personnel for not giving up on this request and helping with the solution. Hopefully this feature will be added soon to a release version, but until then, this patch should do the job.

Hopefully, this post will save many developers hours or days of troubleshooting, and please share this article, as many more developers will thank you. Also remember to comment if this worked for you, and give back by blogging, tweeting, and posting on Sitecore forums about any bugs and fixes you find yourself!


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.