Sharepoint search results are ordered incorrectly when called from code

Sam Kenny
Sam Kenny’s a(musing)
3 min readApr 17, 2013

This particular problem has not been fully resolved. I found a workaround — see lower down — but the issue itself is still perplexing. If anyone has any views or opinions please feel free to comment.

The Application

We have an application that displays the results of a Sharepoint search on a website. The search is a list of articles displayed in reverse chronological order and include date, link and author. The results are displayed both on a Sharepoint page internally and on a ASP MVC3 website — primarily designed for mobile devices but works from anywhere. The web page displays 20 rows at a time.

The problem

The website has been working for the last year without issue and then, sometime around the end of March 2013, the sort order of the results became corrupt. The results on Sharepoint itself are fine.

At first, we thought that certain pages in the search were missing. But on closer inspection the pages were returned, but lower down in the sort order. So articles on April 4th 2013 were sorted with articles dated 2011.

Other notes:

  1. Not all articles for one date were mis-sorted. i.e. some articles from April 5th were sorted correctly, while other articles for April 5th were sorted to be alongside 2011 or 2012 articles.
  2. There was no conceivable difference in the articles. They all are structured, written and dated in the same way.
  3. The problem did not appear to be related to the date being treated as a number or string — because of the symptoms described in #1.

Investigation

At first, I thought that perhaps the Sharepoint search index had got corrupt. I reset the index in Central Administration and recrawled — but this only made the sort worse. Where before certain articles appeared to be missing but were simply mis-sorted, the general sort order was correct. After resetting the index, however, articles from November 2011 were first followed by some of March 2013. It appeared to be very random

In the code, the query for the search results looks like this:

<QueryPacket Revision="1000">
<Query>
<Context>
<QueryText language="en-US" type="MSSQLFT">Select Size, Rank, Path, Title, Description, Write, Author FROM Scope() WHERE (("SCOPE"='MySearch')) ORDER BY "Write" DESC</QueryText>
</Context>
<OrderBy>
<FieldRef Name="Write" />
</OrderBy>
<SupportedFormats Format="urn:Microsoft.Search.Response.Document.Document" />
<ResultProvider>SharepointSearch</ResultProvider>
<Range>
<StartAt>1</StartAt>
<Count>20</Count>
</Range>
<ImplicitAndBehavior>true</ImplicitAndBehavior>
<IncludeHighConfidenceResults>true</IncludeHighConfidenceResults>
<IncludeSpecialTermResults>true</IncludeSpecialTermResults>
<IncludeRelevantResults>true</IncludeRelevantResults>
<EnableStemming>true</EnableStemming>
</Query>

Taking the query and running it in a tool — e.g. Mossman’s FAST for Sharepoint Query Tool — produced the same results as the web app — with the results mis-sorted. Quite what the difference is between Sharepoint parsing the results and our app I am not sure. But something is different — and the difference was introduced on or about the end of March.

Things I tried (to no avail):

  1. The code originally did not include the OrderBy clause in the XML. It had been working fine without it — but we tried it anyway.
  2. Putting the OrderBy before or after the query did not change the results.
  3. Explicitly stating the date and sort order in the FieldRef in the OrderBy:
  • <FieldRef Name=”Write” Type = ‘Date’ Ascending=’False’/>
  1. Re-running full crawls
  2. Restarting the Sharepoint server.

Workaround

The workaround, embarrassing as it sounds, was to increase the count from 20 to 40. All the items on the search results were then displayed correctly with the most recent first. The sort order was actually correct if the count was 31 — is this significant given there are 31 days in March ?? — but decided to play it safe and stick with 40.

--

--