How to display values from custom managed properties in search results – option 2

This is a blog post in the series "How to change how search results are displayed." To demonstrate how you can customize your search results, I'll use examples from an internal Microsoft Search Center.

For an overview of the blog post in this series, go to How to change the way search results are displayed in SharePoint Server 2013.

In the previous blog post, I showed you a simple method for adding a custom icon and values from two custom managed properties to your search results. In this blog post, I'll show you a somewhat more complete method for changing the way your search results are displayed that includes if statements and hit highlighting. We’ll learn:

 

Strategy for killing three birds with one stone - search results version

First, let’s state what we want to achieve:

  • Display values from two custom managed properties
  • Apply hit highlighting to the two custom managed properties
  • Get automatically improved relevancy for our search results

Before I show you how to do this, let's look at the strategy we want to follow to accomplish this.  If it gets a bit complicated, please try to hang in there. Hopefully it'll be clear by the end of this blog.

First, remember how we can think about hit highlighting:

How to think about hit highlighting

  1. The managed properties that are listed in the Hit-highlighted properties (JSON) section of the Search Results Web Part and the "magical summary" property are passed to the HitHighlightedProperties property.
  2. All values of the HitHighlightedProperties property are passed to the HitHighlightedSummary property. 
  3. A truncated version of the values in HitHighlightedSummary is displayed in the Search Results Web Part with three dots at the end.

Also remember that each item display template contains a reference to the Item_CommonItem_Body display template, and that this template contains an onlick method that will result in automatically improved relevance based on the end-user’s click behavior.

Reference to Item_CommonItem_Body display template

So our strategy is simply this: create variables in the item display template that will be passed on and rendered by the Item_CommonItem_Body display template.

Specifically, that means that we have to do the following:

  • Add the custom managed properties that we want to display in our search results to the Hit-highlighted properties in the Search Results Web Part.
  • Add the custom managed properties to an item display template.
  • In the item display template, create a new variable that will be used by the property HitHighlightedSummary to display our two custom managed properties with hit highlighting.
  • In the item display template, leave the reference _#=ctx.RenderBody(ctx)=#_ so that the Item_ComonItem_Body display template will render the search result. This ensures that we get automatically improved relevancy.

OK, now let’s take it step-by-step, with examples of how I did this for my scenario.

 

How to display values from custom managed properties with hit highlighting, and get automatically improved relevancy

First you have to find the managed property names that correspond to the custom site columns that you want to use. I showed you how to do this in a previous blog.

Then you have to do some configuration on the Search Results Web Part. Here's what you should do:

  1. On the Search results page, choose the Settings menu, and then choose Edit Page.
  2. In the Search Results Web Part, choose Web Part Menu --> Edit Web Part.
  3. In the Web Part tool pane, choose to expand the Display Templates section and then select Use a single template to display items. This will allow you to make changes to the Hit-highlighted properties (JSON) field.

Select Use a single template to display items

  1. In the Hit-highlighted properties (JSON) field, use the following format to add the custom managed properties that you want to add hit highlighting to:
    "<Managed property name>"

    In my scenario, I wanted to apply hit highlighting to the ContentSummaryOWSMTXT and the owstaxIdTechnicalSubject managed properties.

Added two properties to the Hit-highlighted properties section

  1. Choose Apply to save the changes. This will close the Display Templates section.
  2. Choose Display Templates to reopen the section, and choose Use result types to display items.

Select Use result types to display items

  1.  Choose OK and save the page.

Then you need to add the custom managed properties to an item display template. Here’s what you should do:

  1. Open the item display template that belongs to the result type for which you want to customize search results.

    In my scenario, this was TechNet content.

  2. In the item display template, in the ManagedPropertyMapping tag, use the following syntax to add the custom managed properties that you want to display:
    '<Current item property name>':<Managed property name>'

    In my scenario, I wanted the values from the managed properties ContentSummaryOWSMTXT and owstaxIdTechnicalSubject to be displayed in the search result. To make the file easier to maintain, I named the current item properties the same as the managed properties.

Two managed Properties added to display template

Then you need to create variables in the item display template that will be used and rendered by the Item_Common_Item_Body display template.  Here's what you should do:

  1. Because you have no guarantee that the values of your custom properties will contain any of the entered query words, that is, hit highlighting will not be used, you have to create variables that ensure that the value of your custom properties will be displayed regardless of hit highlighting.

    The following screenshots show how I created two such variables for my custom properties ContentSummaryOWSMTXT and owstaxIdTechnicalSubject.

Variables for custom properties

  1.  In addition, I added a similar variable for the Title property. If you don’t add this, the search results will not be rendered.

Variable for Title property

  1. The last step you have to do in the item display template is to create a variable that will override the HitHighlightedSummary property used to display the values.

Variable for HitHighlightedSummary property

  1. Save the item display template.
  2. NOTE: You do not need to do this step if you are using SharePoint Online.
    Go to Site settings --> Search Result Types. Notice that a Property Sync alert is displayed.

Property Sync alert on Managed Result Type page

This alert is displayed because we added managed properties to an item display template (what we did in step 9). To update the result types with the newly added managed properties, choose Update.

Update Properties from result types

IMPORTANT! If you don't do this update, the newly added managed properties will not display in your search results.

After I made these changes, when users entered a query in the Search Center, the search result included:

  1. A custom icon.
  2. The value of Title with hit highlighting.
  3. The value of ContentSummaryOWSMTXT with hit highlighting.
  4. The value of owstaxIdTechnicalSubject. The query words did not match the property value, however because of the variable that I created in step 10, the value is displayed.
  5. A link to the item in the list

Custom search result

I wanted to make one little change to how the value for owstaxIdTechnicalSubject was displayed. I wanted to give users a bit more context as to what this value represents, so I decided to add the text "Technical Subject:" before the value. Also, because this value is not always present for all list items, I decided that it should only display when a value was present.

To do this, I made a change to the variable that overrides the HitHighlightedSummary property:

Updated variable for HitHighlightedSummary

Notice that I added a slightly different color to the text "Technical Subject:". With this addition, the final search result is displayed like this:

Updated custom search result

In our planning, we had decided that we wanted 6 different result types. After creating the TechNet content result type and display template, it was very easy to copy this work over to the other 5 result types. 

And here’s the result:

Final customized search results page

 

So now that we have changed the way search results are displayed, the next step is to change the values that are displayed in the hover panel.

 

Next blog post in this series
How to display values from custom managed properties in the hover panel