Posted by: R Manimaran | November 4, 2014

SharePoint 2010 Creating Custom Ranking Model – with Example – Part 2

SharePoint 2010 Creating Custom Ranking Model – with Example – Part 2

In the part 1 we have created the custom Ranking model which holds our custom managed property to give more weight-age while doing search.

Let’s use the Keyword Query class to test our ranking model. Add reference to the below namespaces.

using Microsoft.Office.Server.Search.Query;

using Microsoft.Office.Server;

public void Search()


//Keyword provided for the Search

string userKeyword = "Manimaran";

KeywordQuery keywordQuery = new KeywordQuery(SPContext.Current.Site);

keywordQuery.ResultTypes = ResultType.RelevantResults;

keywordQuery.EnableStemming = true;

keywordQuery.TrimDuplicates = false;

keywordQuery.IgnoreAllNoiseQuery = true;

//managed properties







//Set my Custom Ranking model Id

keywordQuery.RankingModelId = "1786318d-cbcc-4c06-9e9f-fe5d3f12279e";

//RankingModel is my Custom Search Scope

keywordQuery.QueryText = "\"" + userKeyword + "\" scope:\"RankingModel\" IsDocument:\"True\"";


ResultTableCollection searchResults = keywordQuery.Execute();

DataTable relevantResults = new DataTable();

relevantResults.Locale = CultureInfo.InvariantCulture;

if (searchResults.Exists(ResultType.RelevantResults))


ResultTable relevantResultsTable = searchResults[ResultType.RelevantResults];

relevantResults.Load(relevantResultsTable, LoadOption.OverwriteChanges);


if (relevantResults != null && relevantResults.Rows.Count > 0)


DataView dv = relevantResults.DefaultView;

DataTable dtOrdered = dv.ToTable();



When I ran the above code I got the results as below.


Thanks to Mikael Svenson for the guidance through email.

Both Part1 and Part 2 are available as document in the below location.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: