Tuesday, December 13, 2011

Returning Best Bets and Visual Best Bets with KeywordQuery and FAST Search for SharePoint

When using the Search Center the Best Bets or Visual Best Bets functionality works like a charm, but I have not been able to get it to work using the KeywordQuery class in SharePoint.

I had done some research previously on the matter after being contacted by Xavier Vanneste who blogged about this (http://blog.xvanneste.com/Lists/Billets/Post.aspx?ID=82), but was not able to figure it out at the time.

I’m currently writing sample code for my upcoming book on FS4SP and I had to figure this out. Time to bring out the tools!

One hour with the Visual Studio Reflector plugin and setting break-points internally in the SSA I finally figured it out. And in a way it was obvious.


All keyword settings with FAST requires a Search Setting Group and you will have one group for each site collection in your SharePoint farm. The ID of the search setting group is the GUID of the site collection. And this ID is the solution.

KeywordQuery has a property named “UserContextGroupID”, and MSDN states:
Gets or sets the identifier used to group keywords used for matching best bets and visual best bets. This property specifies an identifier used to group keywords used for matching best bets and visual best bets.
By assigning the UserContextGroupID property and requesting to get back SpecialTermResults it is now working like a charm.

Here’s a small sample which will return a Visual Best Bet assigned to the term “FAST Search for SharePoint”.

var searchProxy = (SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy(SPServiceContext.Current);
KeywordQuery kq = new KeywordQuery(searchProxy);
kq.QueryText = "FAST Search for SharePoint";
kq.ResultsProvider = SearchProvider.FASTSearch;

// specify best bets/visual best bets
kq.ResultTypes = ResultType.SpecialTermResults;
// specify the Search Settings Group ID
kq.UserContextGroupID = SPContext.Current.Site.ID.ToString();

ResultTableCollection resultTableCollection = kq.Execute();
ResultTable visualBestBetsTable = resultTableCollection[ResultType.VisualBestBetsResults];
foreach (DataRow row in visualBestBetsTable.Table.Rows)
{
    string name = (string)row["Name"];
    string uri = (string)row["Uri"];
    string description = (string)row["Description"];
    string keyword = (string)row["Keyword"];
    string teaser = (string)row["Teaser"];
    string contentType = (string)row["TeaserContentType"];
}

22 comments:

  1. I discovered your blog site on google and check a few of your early posts. Continue to keep up the very good operate. I just additional up your RSS feed to my MSN News Reader. Seeking forward to reading more from you later on!…
    sharepoint developers blog

    ReplyDelete
  2. looking forward to your upcoming book!

    ReplyDelete
  3. Hi Mikael, when is the book released for purchase?

    ReplyDelete
  4. Hopefully around February, and I will announce it once I know for sure :)

    ReplyDelete
  5. Thanks for the FAST Search info. I've pre ordered the new book via Amazon and it states the release date is April 2. Is this still true or will it be earlier? This book could probably hit the New York Times top seller as there is nothing like it. You may want to also have a look at the cover shot they have available on Amazon for the new book because it shows a cover of Office 360. Thanks again.

    ReplyDelete
  6. According to O'Reilly it will be out beginning of March, which is the schedule we're working at.

    ReplyDelete
  7. Hello Mikael,

    We are working with Fast Search for Sharepoint 2010 and had some backend setup done with creating some managed properties e.g. BestBetDescription, keywords etc.

    From the front-end part we are creating an application what will fetch all these properties and display in a grid.

    However while querying the backend we are NOT getting these managed properties (BestBetDescription) along with other properties such as Title, URL etc.

    We are not able to figure out what's the problem since we all are new to this and learning things everyday.

    ReplyDelete
  8. Sayed,
    It would help to know what you have tried in your code so far in order to help.

    ReplyDelete
  9. Hi mikael,

    I have created keywords,best bets ,visual best bets under site collection but its not coming in the search results. Is site collection the right place for creating those? Any idea why its not resulting in the search results? Any XSLT to be modified for getting this?

    Regards,

    Ananth.R.

    ReplyDelete
    Replies
    1. Hi,
      Is your issue using the KeywordQuery class or using the SharePoint Search Center?

      Delete
  10. Replies
    1. Hi,
      If you add a keyword and best bet on the site collection it should appear if you have configured the core results web part to show them.

      Make sure there is a default search group created for your site collection.

      Get-FASTSearchSearchSettingGroup -Name

      where siteid is the guid of your SPSite.

      Delete
  11. Hi,

    Any suggestion / links on how to configure it on core results web parts.

    Regards,

    Ananth.R.

    ReplyDelete
    Replies
    1. Take a look at http://office.microsoft.com/en-us/fast-search-server-help/manage-fast-search-keywords-HA010381994.aspx on how to work with keywords. And take a look at http://office.microsoft.com/en-us/sharepoint-server-help/customize-the-search-center-site-HA010382279.aspx on how to configure the search center.

      It's all very straight forward.

      Delete
  12. Thanks Mikael...Few more queries

    [1] Is it possible to configure fast search for sharepoint content DB?

    [2] For Security trimming : I am storing the security descriptor of the user who logged in. Is it possible to store multiple users login details with semicolon separation...if i do that way whether security trimming will work?

    [3] While connecting to Sharepoint designer from Fast search center page the SPD is asling for username and password and even if i give the correct one its not proceeding? Any clue?

    Regards,

    Ananth.R.

    ReplyDelete
    Replies
    1. Hi,
      1) Not sure what you mean exactly
      2) Do you want to store ACL's, and run searches on the users behalf? If so, no. The only way to search on another users behalf is to execute the query under impersonation
      3) Not sure why.. the FAST Search Center should not be special in any way compared to other sites. Do you run it as a separate site collection or a subsite?

      -m

      Delete
  13. Hi,

    [1] Already having fast search configured and running with custom DB (Sql Server). Like to know whether fast search can be configured for sharepoint's content DB(sharepoint's database)

    Ananth

    ReplyDelete
    Replies
    1. Are you talking about indexing a custom db, or the property db used by fs4sp?

      I still don't understand what exactly you are asking.

      The content db is part of sp.. what do you want to do with it?

      -m

      Delete
    2. Yes the content db...which is like for eg. WSS_ALLLists .

      Fast search with sql server is already working. Along with i got the requirement to use Fast Search for content db which is part of sp.

      Pls suggest and share your thoughts on that.

      Delete
    3. If you want to index sharepoint content, then set up a crawl of it using the Content SSA. Don't use the sql crawler.

      Delete
  14. In sharepoint 2010 - I created a sample web app,site collection and site and then integrated Fast Search with content SSA,Query SSA etc and its working fine. Now i got the real application and the database.How i can add/integrate those into sharepoint. Please suggest some ways/links?

    ReplyDelete
    Replies
    1. When you have the content and query ssa set up, create a new search center based on the FAST site template, and you have a search UI using FAST.

      Delete