Sunday, June 2, 2013

How to: Change the default sort of a Search Result Web Part

When using the Content Search web part you can modify the query in the query builder. By default it lists recently changed items but often you want to change the query using advanced mode.

image

When clicking advanced mode, the default sorting is set to Rank, and you can click the sorting tab, choose LastModifiedTime in descending order, and you end up with the latest items with the filter you decide to add.

So far so good. Say you have either developed display templates for your search page, or want to do content by search in Office 365, and you want to use the Search Results web part instead which features more or less the same capabilities as the Content Search web part.

The procedure is almost similar. Drop a Content Search web part on the page and enter the query builder. In my case I want to limit the results based on one document library in particular and will use the query. I remove the {searchboxquery} token and enter:

(contentclass:STS_ListItem OR IsDocument:True) path:http://intranet.contoso.com/Documents

Further clicking “Test query” lists all results sorted by rank. I click the sorting tab, choose LastModifiedTime and the test results show as expected, sorted by the newest items on top.

image

Next I click ok, and save the web part settings, save the page, and…… the results are still sorted by rank!!

There is however a workaround. Instead of changing the sorting using the query builder, you have to change the UI sorting options for the web part.

When editing the web part, expand the Settings section. Check the “Show sort dropdown” option and replace the content with only one value, the one sorting my newest items on top. After you have modified the JSON value, click “Apply” to save the web part changes. Next uncheck the option again, and save the web part changes. The sorting now sticks, but you’re not showing the sort dropdown.

imageimage

Don’t know if this is a bug or by intention, but is sure bugged me for a couple of hours.

6 comments:

  1. This had me a bit confused as well. Turns out you need to go to the Settings tab in the query, and say "Don't Use Query Rules" from site settings. Boom, it works... :-)

    ReplyDelete
    Replies
    1. Nice workaround...but often you want query rules to interact with the query. Of course, then you can set the sorting in the rule.

      Delete
  2. Thanks Mikael. It worked like a charm!

    ReplyDelete
  3. Hi Mikael,
    I used the same web part option as you mentioned (Don’t know if this is a bug or by intention, but is sure bugged me for a couple of hours.) Above but now I found that this option not working any more can you please check.

    ReplyDelete
    Replies
    1. Sorry.. no time. You could inspect the request made, and see what is sent over on the query. Set the web part to run async in order to see the query.

      Delete
    2. Thanks for you time @Mikael!! I will check at my end.

      Delete