Querying Multiple Federated Locations in Enterprise Search with the QueryManager class

Posted Wednesday, August 25, 2010 11:54 AM by CoreyRoth

Last week, I showed you how to use the QueryManager class to query SharePoint Enterprise Search.  In that post, I mentioned that we can actually use the QueryManger class to harness the true power of federated search and query multiple locations at once.  As you will see today, it’s pretty easy to do, but you might have to do some work with the XML you get back.  We’ll start with the code from last week, and then add the search results from the included Bing.com Internet Search Results provider.  To add another location, we just add another location to the LocationList object.

Location internetLocation = new Location("InternetSearchResults", searchProxy);

locationList.Add(internetLocation);

Remember that the name of the Location comes from the Location Name field on the federated location.  Refer to the table in the QueryManager post for more information.   Using the code from last week, we put all this together and it looks like this:

// get the query and settings service proxy

SearchQueryAndSiteSettingsServiceProxy settingsProxy = SPFarm.Local.ServiceProxies.GetValue<SearchQueryAndSiteSettingsServiceProxy>();

// get the search service application proxy by name

SearchServiceApplicationProxy searchProxy = settingsProxy.ApplicationProxies.GetValue<SearchServiceApplicationProxy>("Search Service Application");

QueryManager queryManager = new QueryManager();

// add the federated location we want

LocationList locationList = new LocationList();

Location localSearchLocation = new Location("LocalSearchIndex", searchProxy);

locationList.Add(localSearchLocation);

// add results from bing

Location internetLocation = new Location("InternetSearchResults", searchProxy);

locationList.Add(internetLocation);

 

queryManager.UserQuery = "accounting";

queryManager.Add(locationList);

queryManager.IsTriggered(locationList);

XmlDocument xmlDocument = queryManager.GetResults(locationList);

System.IO.File.WriteAllText("results.xml", xmlDocument.InnerXml);

Console.WriteLine(xmlDocument.InnerXml);

Console.ReadLine();

As you can see, it’s pretty simple to add another location.  What do the search results look like though?  Well, the results may not be what you expected.  It just appends the second location’s results to the XML document.  What is bad is that the schema isn’t the same. Since Bing results are coming in via an OpenSearch provider, it just gives you the raw RSS feed of the results.  I won’t post the entire XML document here, but you should get the idea of what I mean.

  <isdocument>False</isdocument>

  <picturethumbnailurl></picturethumbnailurl>

  <serverredirectedurl></serverredirectedurl>

</Result>

<TotalResults>24</TotalResults>

<NumberOfResults>10</NumberOfResults>

<channel>

  <title>Bing: </title>

  <link>http://www.bing.com:80/search?q=accounting</link>

  <description>Search results</description>

  <image>

    <url>http://www.bing.com:80/s/a/rsslogo.gif</url>

    <title>accounting</title>

    <link>http://www.bing.com:80/search?q=accounting</link>

  </image>

  <copyright>Copyright © 2010 Microsoft. All rights reserved. These XML results may not be used, reproduced or transmitted in any manner or for any purpose other than rendering Bing results within an RSS aggregator for your personal, non-commercial use. Any other use of these results requires express written permission from Microsoft Corporation. By accessing this web page or using these results in any manner whatsoever, you agree to be bound by the foregoing restrictions.</copyright>

  <item>

    <title>Accountancy - Wikipedia, the free encyclopedia</title>

    <link>http://en.wikipedia.org/wiki/Accounting</link>

    <description>Accountancy is the art of communicating financial information about a business entity to users such as shareholders and managers. [1] The communication is generally in the ...</description>

    <pubDate>Tue, 24 Aug 2010 08:47:00 GMT</pubDate>

  </item>

This may not be ideal, but if you need the data, you can get it.  You may just have to work with the XML a bit.  Luckily LINQ to XML makes that pretty easy.  What about if you add a third location?  Well it just appends it right onto the end.  I added another SharePoint location and it simple adding Result elements right onto the document.  What I don’t like about it is that there is nothing in the Result element that tells you what location it came from. 

You know reading that copyright message on the Bing results makes me wonder.  It specifically says that the results can only be used with an RSS aggregator for personal, non-commercial use.  I would venture to say most uses of SharePoint are commercial.  Does this mean we are not supposed to use Bing results there?  Perhaps that is a question for the forums.

Even if you have to work with the XML a bit to get the results you need, I think this is very powerful.  It’s nice that you can get results from more than one location so easily.  I’ve attached an XML file to this post with the complete result set from three locations if you want to examine it further.

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required)