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

keywordQuery.SelectProperties.Add("EmpId");

keywordQuery.SelectProperties.Add("EmpName");

keywordQuery.SelectProperties.Add("EmpDOB");

keywordQuery.SelectProperties.Add("EmpDOJ");

keywordQuery.SelectProperties.Add("HitHighlightedSummary");

keywordQuery.SelectProperties.Add("Filename");

//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\"";

//execution

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.

CustomResults

Thanks to Mikael Svenson for the guidance through email.

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

Part1: https://gallery.technet.microsoft.com/SharePoint-2010-Creating-e439cee3

Part2: https://gallery.technet.microsoft.com/SharePoint-2010-Custom-b6ca6b3e

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Categories

%d bloggers like this: