How to: Query FAST Search for SharePoint with FQL and the Search Web Service

Posted Tuesday, December 27, 2011 8:04 PM by CoreyRoth

I’ve written many posts on how to query search (Keyword in 2010, Keyword in 2007, QueryManager, Web Service, FQL using Keyword) and I wanted to keep up the tradition and talk about how easy it is to get started using FQL with the Search Web Service.  Our same techniques apply from my previous web service article with only some slight changes.  It may seem simple at first, but it’s the first building block that we need for a series of posts I am starting. 

For today’s example, I am going to build off of the Silverlight 4 application I used at SPC.  I’m using Silverlight, but you could just as well write a console application or call this web service from some other ASP.NET application.  We still want to start by creating a service reference to /_vti_bin/search.asmx. We then create an instance of the web service so that we can use it.  We also bind an event handling method to handle the results of the web service call.

QueryServiceSoapClient queryService = new QueryServiceSoapClient();

queryService.QueryExCompleted += new EventHandler<QueryExCompletedEventArgs>(QueryService_QueryExCompleted);

Once we have a reference to the web service, it’s just a matter of constructing the input XML document and sending it to the web service.  This input XML is typically the same for a basic query.  However, the difference here is in the type attribute of the QueryText element.  We want to specify a value of FQL instead of STRING.

<QueryPacket xmlns="urn:Microsoft.Search.Query" Revision="1000">

  <Query domain="QDomain">

    <SupportedFormats>

      <Format>urn:Microsoft.Search.Response.Document.Document</Format>

    </SupportedFormats>

    <Context>

      <QueryText language="en-US" type="FQL">productline:"*ice"</QueryText>

    </Context>

  </Query>

</QueryPacket>

That is what the XML looks like.  In this case, I am using a simple FQL query to return anything matching the managed property using a wildcard operator. In this case, I was looking for the word Office but you can see how I use “*ice” and still get results. This prefix wildcard operator only works with FQL. If you change the query back to type STRING (keyword), it would not work.  You can store it in a separate file or just build it with a StringBuilder.  We’ll go with a StringBuilder today for simplicity.

queryXml.Append("<QueryPacket xmlns=\"urn:Microsoft.Search.Query\" Revision=\"1000\">");

queryXml.Append("<Query domain=\"QDomain\">");

queryXml.Append("<SupportedFormats>");

queryXml.Append("<Format>");

queryXml.Append("urn:Microsoft.Search.Response.Document.Document");

queryXml.Append("</Format>");

queryXml.Append("</SupportedFormats>");

queryXml.Append("<Context>");

queryXml.Append("<QueryText language=\"en-US\" type=\"FQL\">");

queryXml.Append("productline:*ice");

queryXml.Append("</QueryText>");

queryXml.Append("</Context>");

queryXml.Append("</Query>");

queryXml.Append("</QueryPacket>");

We then need to make the call to the Search web service.  Remember that you can call QueryEx to get a DataSet and Query to get an XML document.  I always prefer to work with the raw XML since I can use LINQ to XML with it.

queryService.QueryExAsync(queryXml.ToString());

We then jump implement our event handling method QueryService_QueryExCompleted.  Remember, that the XML we want is always in the node element in position 1.  We can bind this data to a textbox, like the snippet below or you can use my example from the SPC article to bind to a grid.

ResultsTextBox.Text = e.Result.Nodes[1].ToString();

Here is what my example looks like.

FQLSilverlight4WebServiceResults

As I said it’s pretty simple.  One thing I have noticed with the Silverlight application is that if you get the syntax of an FQL query wrong, it won’t return gracefully, it will throw a NotFound exception.  Who knows why?

FQLSilverlight4NotFoundException

In the above example, I didn’t specify the managed property with lower case letters.  It must be lower case or you will always get an exception.  This varies from the KeywordQuery syntax.  I hope this example helps.  Remember, if you do need help getting started with this application, take a look at the sample code I provided in the previous article.  In the following articles, I’ll start demonstrating how to leverage some of the more advanced FAST Search for SharePoint features using the web service.  I figured it would be good to write this article as a starting point first though.

Comments

# Sharepoint Updates December-28-2011 | SDavara&#039;s Sharepoint Knowledge Center

Pingback from  Sharepoint Updates December-28-2011 | SDavara&#039;s Sharepoint Knowledge Center

Leave a Comment

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