How to use Query rules and display templates to detect the intent of your users

How to use Query rules and display templates to detect the intent of your users

Rate This
  • Comments 1

Hi,

I'm pretty sure you have tried to search in Google or Bing the answer to a math operation, and you got an instant result that matches exactly that.

 

When you read about this scenario,talking about SharePoint 2013, "A search result, suggestion, or recommendation is more relevant when it better satisfies the intent of the person who issues the query", you can see how powerful can be to "understand" what your users are trying to find, and as a result give them that specific answer.

I will show you how to include that behavior in SharePoint 2013 using the new features. Specifically Query Rules and Custom Display Templates. I will not cover here what are those features, you have great info in Steve Peschka blog (well may be next year I will talk about just a little bit ;)).

Let's show the result and then go through the steps:

 

Here the user has asked for the result of 12*5, this was a match for the configured condition (Query Rule) and then a specific "relevant" result was shown (Result Block) with a custom visualization (Display Template). You can just start imagine different examples where you can add Instant answers to your users in your organization:

  1. Translate content with action words, "Translate some_fancy_word to spanish"
  2. Integrate Line of Business applications, "Create a new support case" "Projects with Customer X"
  3. Get specific information, "Last report for Customer Y"

to mention some examples.

So to set up the scenario with the math operation, the following elements will need to be created and deployed,

Item_Calculator.html (and a file associated with it named Item_Calculator.js)

This will display the calculator and will manage the math operations. A summary of the most relevant parts (Item_Calculator.html):

Head tag

General properties (ex. display template name), design view styles and the necessary properties from the search service. In the scenario chosen none specific managed property will be used as  the result is based on just the query.

Body tag

Before calculators' container (div) the styles and JavaScript code will be referenced, in order to add the functionality and visualization code.

<script>
$includeCSS(this.url, "~sitecollection/Style Library/calc/style.css");
$includeLanguageScript(this.url, "~sitecollection/Style Library/calc/script.js");
</script>
 

then all the specific HTML code that will be rendered will be inside the <div id="Item_Calculator">.Here a callback to the final calculation, after control rendering, will be executed with the user's query.

<!--#_
AddPostRenderCallback(ctx, function()
{
calculate(document.getElementById('box').value);
});

if(!$isNull(ctx.DataProvider)){
var calValue = ctx.DataProvider.get_currentQueryState().k;
_#-->

the user's query will be put in the calculator's box with its value

<input id="box" type="text" value="_#= $htmlEncode(calValue) =#_">

Item_Calculator.html and Item_Calculator.js should be placed in "/_catalogs/masterpage/display templates" OOB folder

script.js and style.css should be placed in "/Style Library/calc/" custom folder

Query Rule

On the other hand, a rule will need to be configured matching this query and using the result.

You need to go to Site Settings->Query Rules and create a new Rule based on a source (in my case Local SharePoint Results), with the following information: a name, the condition (Advanced Query Match as we want to include a regular expression that matches the operations: [^d]*[/*/+][^d]*) and the result block; with defaults, one result and our Calculator Item Display Template.

 

This will allow the system to detect when a math operation is requested and display the result block previously configured.

You can download the source code from codeplex.

 There were some things that came out doing this, that I will talk about in next posts: eval is evil, browser support of JSON, debugging display templates, display templates pipeline and circular references

Ciao!

Leave a Comment
  • Please add 4 and 4 and type the answer here:
  • Post
  • Hi Carlos, thanks for your post. I'm interested in some (all) of the topics for the next post, eval is evil and browser support of JSON. Did you wrote something about these topics?

    best regards

    Riccardo

Page 1 of 1 (1 items)