Friday, March 11, 2016

Sitecore Lucene highlight search term for best matching field

In a previous post I outlined an example of some code to highlight the search term in the search results using Lucene in Sitecore. This works well if you have one field you want to display on the search results page (the page description for example). But if you have multiple fields which could contain the search terms, it's a good idea to check them all to see which has the best match.
Luckily the GetBestTextFragments method in Lucene will provide a score for each piece of content you search against, which allows you to show the most relevant content.

When passing through the list of content to search against, you should send the most relevant fields first. Because if there are no matches the first piece of content is returned, and if 2 pieces of content have the same score, the one higher in the list will be sent back.

No comments:

Post a Comment