Search Guys

Everything search, including product commentary and guidance, search strategies and general technical guidance

Mucking with search summaries in SP2013

Mucking with search summaries in SP2013

  • Comments 1
  • Likes

For a project I’m working on I needed to add some redaction to the hit highlighting summaries of the search results.   If I see anything that looks like a social security number, then replace the first five digits with asterisks.

At first this looked somewhat daunting because looking in the item templates, all the action on rendering the summary is inside of the ctx.RenderBody method:

<div id="_#= $htmlEncode(itemId) =#_" name="Item" data-displaytemplate="DefaultItem" class="ms-srch-item" onmouseover="_#= ctx.currentItem_ShowHoverPanelCallback =#_" onmouseout="_#= ctx.currentItem_HideHoverPanelCallback =#_">
    <div id="_#= $htmlEncode(hoverId) =#_" class="ms-srch-hover-outerContainer"></div>

After a little poking and prodding, it turns out all you need to do is modify the HitHighlightedSummary value before the RenderBody is called.  Add your code after the $setResultItem as follows:

$setResultItem(itemId, ctx.CurrentItem);

// Redaction of Social Security Numbers
var snippet = ctx.CurrentItem['HitHighlightedSummary'];
if (snippet) {
    var re = /(?:<c0>)?[0-9]{3}(?:<\/c0>)?-(?:<c0>)?[0-9]{2}(?:<\/c0>)?-((?:<c0>)?[0-9]{4}(?:<\/c0>)?)/g;
    snippet = snippet.replace(re, '***-**-$1');
    ctx.CurrentItem['HitHighlightedSummary'] = snippet;

The regular expression is somewhat complicated because it needs to account for highlighting tags (). Note that you can add to the HitHighlightedSummary yourself and can use the tags if desired.

  • BTW, the ctx.RenderBody(ctx) is inserting the content of the Item_CommonItem_Body.html.

    If you need to customize that, simply copy the contents of Item_CommonItem_Body into your template and replace it with the ctx.RenderBody(ctx).

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment