Working with FAST Search For SharePoint 2010 Document Promotions Programmatically

As much as I love working with the FAST for SharePoint 2010 search engine, finding documentation for it online is quite a challenging task. This is why I’m going to try and document as much as possible about the little ‘gems’ I find while working with FAST.

Using FAST for SharePoint you can define documents to be promoted within the search results list, indicating that they are highly relevant results for a particular keyword or its synonyms. The promoted documents will appear at the top of the search result list.

While this feature works great when using OOTB Core Search Results web part, it fails miserably when using an extended Core Search Results web part (a web part that inherit from the base Core Search Results web part and extend its functionally, such as giving the developer full control over the FQL (Fast Query Language) query sent to the server).

Today I had a client who uses such web part and thus has to get the promoted documents urls programmatically so he can append an xrank property to the FQL query. This was much trickier task than expected. To get the promotions you first have to get the Search Setting group for your site. This can be done using the following code:

var ssaProxy = (SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy(SPServiceContext.Current);
if (ssaProxy.FASTAdminProxy != null)
{
    var fastProxy = ssaProxy.FASTAdminProxy;
    KeywordContext keywordContext = fastProxy.KeywordContext;
    var searchGroup = (from x in keywordContext.SearchSettingGroups where x.Name==SPContext.Current.Site.ID.ToString() select x).First();  
}

Once we have the search group, we need to get the keyword itself:

var getKeyWord = from x in searchGroup.Keywords where x.Term == searchKeyword select x;

Where searchKeyword is the term that was searched.

Keyword keyword = getKeyWord.ElementAt(0);

Now all we have to do is go through the collection of promoted items for that keyword, and look for a PromotedItem of type PromotedDocument:

foreach (var promo in keyword.Promotions)
{
foreach (var item in promo.PromotedItems)
{
if (item isPromotedDocument)
{
// your logic here
}
}
}

To get the URL for the promoted document use the following code:

(item asPromotedDocument).DocumentId

Once you have the url you can add it to your FQL query like

xrank(path:equals(“<DocumentId>”), boost=100000, boostall=yes)

I hope this article saves you a healthy dose of slamming your head against the wall while trying to figure out where the promoted documents urls are hiding!

Leave a comment