Friday, June 27, 2014

S15E07: Query Rules – Get things in order

Want the book? Go get it!

This is the seventh episode in the series “SharePoint Search Queries Explained - The Series”. See the intro post for links to all episodes.

In the “Rewrite and paraphrase” post I discussed the concepts of recall and precision which has to do with bringing back relevant items for a search query. When you have your golden set of items, the next step is to sort them in the best order possible to ensure the most relevant items are in your top hits or first page of results.

Tip: Increase the default number of items to display to 25 or 50. Ten items is not a lot and people are getting more and more used to scrolling these days.

There are four ways to sort a result in SharePoint:
  1. Sort the results based on a ranking profile
    • You may also influence ranking by using the XRANK keyword (dynamic ordering)
  2. Sort the results based on a managed property ascending or descending
  3. Sort the results based on a sorting formula
  4. Sort the results randomly
    • This is accommodated using a special sorting formula

Using query rules, the first two options above are available (rank and managed property) using the Query Builder interface in SharePoint. If you want to sort by formula in a query rule you will have to export the search configuration, edit the XML manually and import it back in. The property field will then show empty in the query builder.

    <d8p1:Value i:type="d4p1:SortCollection">


You can have a total of  five sorting levels defined for a query rule. If you are using rank as a sorting level, it has to be the first level, and you can only specify one ranking level.


More Information: The schema definition for query rule sorting as specified in SortDirection at MSDN states that in order to use a formula for sorting, you should specify FQLFormula as the SortDirection property. While this work, it won’t let you specify a direction of the sorting. If you want to specify which direction your sorting should, keep Ascending/Descending as the SortDirection, and write your formula in brackets [] as per Sort search results by a formula expression, and not with regular FQL as stated in the specification. This is equal to the code shown earlier in this post where the random formula was used.

There can only be one (rule to sort them all)

One very important thing to note if you are changing the sort order using Query Rules is that only ONE rule can apply sorting to your results. If you have more than one rule triggering and changing the sort order, then neither will be applied.

If you have more than one rule which sets sort logic, where both can in theory trigger, then you need to add the query rules to a group, put them in the right order, and set STOP for each rule. This implies that sorting rules should be ordered to the bottom of all your rules.


Sorting by rank

The default sorting in SharePoint is to use the default rank profile. It is possible to create custom rank profiles, but that’s a full series by itself left for later.

The dynamic part however is very much within grasp by using XRANK. The easiest route to using XRANK for promoting or demoting items is to add a dynamic ordering rule using the query builder. In the sample below I am promoting items which contain my first name Mikael.


Clicking the TEST tab will reveal the final query for this setting.


The drawback with using the Sorting builder is that you cannot make variable ranking conditions (shown in Sample 3 of episode three). If you want full control you can experiment by using the builder, then take the final query and paste it into the Query box yourself.


If you want last modified date to influence your ranking, take a look at my previous post about freshness boost.

Sorting by managed property

Any managed property marked as sortable can be used to sort your results. Typically news articles should be sorted by last modified time in descending order, to show the newest articles first.

When searching for people in a directory you might want to first sort on last name, then on first name, as apposed to social distance.

Sorting by formula

If you have a product catalog with both the price you are paying and the price you are selling it for in separate managed propertioes, you could sort items by highest margin.


Formula sorting can be applied to managed properties of type Integer, Decimal and Datetime. the properties also have to be marked as sortable.


How you order search results is an important part of presenting your search results. By transforming business rules to query rules, you can change how items are ordered to create better search experiences for your end users. It’s also possible to set sorting both at result sources and web parts directly, but using rules you can trigger sorting based on different criteria.