3

I am working with SharePoint 2013 search. I am using a Customized display template (using Result Type) to display the search results. It all works fine.

I would like to implement Hit-Highlighting to my search results.

In my custom display template, I have both 'HitHighlightedSummary' and the 'HitHighlightedProperties' properties in the ManagedPropertyMapping section.

I am displaying the document title in the search result which I want to highlight.

I have the title configured in the JSON Hit-Highlighting section in the Search Results web part configuration, but the title is still doesn't get highlighted even when the search results matches what gets typed in the search box.

enter image description here

Am I doing something incorrect here?

Please reply.

Thank you. A

2 Answers 2

3

Found the solution. To get the highlighting to work, the code:

var variableName = ctx.CurrentItem.csr_id;

needs to be changed to:

var variableName = ctx.CurrentItem.HitHighlightedProperties;

and then use the 'variableName' to get the value of the property, here in this case it is 'Title'

var titleHitHighlighted = Srch.U.getHighlightedProperty(variableName , ctx.CurrentItem, "Title");

and then in the body of the page, use the variable like so:

<div>
_#= titleHitHighlighted =#_
</div>

Best regards.

1

You would need to add the highlighting of the title in your custom display template as below:

/* start: getting value of hit-highlighted property */

/* use this snippet to extract the value of a hit highlighted property */

/* get the current item id */

var csr_id = ctx.CurrentItem.csr_id;

/* get the hit-highlighted value */

var titleHitHighlighted = Srch.U.getHighlightedProperty(csr_id, ctx.CurrentItem, "Title");

/* get the original value in case nothing was returned in the previous */

if ($isEmptyString(titleHitHighlighted)) {titleHitHighlighted = $htmlEncode(ctx.CurrentItem.Title);}

/* end: getting value of hit-highlighted property */

And to render the html, use it as below:

<!– custom HTML markup to render the title –>

<div class="highlightedTitle">

_#= titleHitHighlighted =#_

</div>

<!– end custom markup –>

Reference - Applying Hit-Highlighting to any property in the Search Results WebPart in SharePoint 2013

3
  • Thank you for your reply. I tried adding your code. The highlighting doesn't seem to work. The code 'ctx.CurrentItem.csr_id' simply returns an 'undefined' value. Is there something I need to check here? Thank you Commented Oct 31, 2016 at 21:21
  • I think what you have done is customize the result type. What you need to do is create a custom display template. ableblue.com/blog/archive/2013/06/05/… here, an example of item person template is displayed is show. You can create a copy of the item_commonitem_body and then customize it as per my code and then apply it to search results Commented Nov 1, 2016 at 7:24
  • Thank you for your reply. The reason why I am using the 'Result type for item' option is that I am displaying some crawled properties in my custom search results. Can't I achieve the Hit-Highlighting using the 'Result type for item' option ? Would I need to choose the 'Item Display Template' instead of the 'Result type for item' option? Thank you Commented Nov 1, 2016 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.