How to: Execute FAST Query Language (FQL) Queries using the KeywordQuery Class
Posted
Wednesday, September 22, 2010 1:07 PM
by
CoreyRoth
Anyone who likes SharePoint Enterprise Search has to love FAST. As a developer, sooner or later you are going to want to take advantage of the FAST Query Language also known as FQL. This lets you truly leverage the power of FAST and perform very advanced queries. I’ve shown you how to use the KeywordQuery class in SharePoint 2010. Now we’re going to expand on that to execute FQL queries. It’s actually easier than you might suspect. It all starts with setting the EnableFQL on the KeywordQuery class to true. Of course, you still have to remember how to put the rest of it together first. :)
Note, there is also another FQL out there known as the Facebook Query Language. If you’ve come here looking for that, you’ve come to the wrong place as I don’t know anything about that. Although maybe I should. :)
When dealing with search, we always start by figuring out what our service application proxy is called. On a typical FAST installation, you are likely to have three different Search Service Applications: FAST Search Connector, FAST Search Query, and a regular SharePoint Search Service Application to handle people search. This means you need to find the right one (FAST Query) in order to actually get some meaningful results. Head to your Service Applications page in Central Administration and take a look.
In my particular case I happened to have named my service application FAST Query SSA. I got this name straight out of the installation guide for FAST, but I have found that people don’t have much consistency on what they name this. If you are not sure which application to use, select one of them and click the Properties button. If it looks something like this, you have found the right one. I also think it’s the only FAST service application that has a proxy, but I could be wrong.
Excellent! So now you know the name of your service application so in turn you know the name of the proxy. Now we leverage the SeachQueryAndSiteSettingsServiceProxy again to get a reference to the SearchServiceApplicationProxy that we need.
// 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>("FAST Query SSA");
These lines are similar to my previous KeywordQuery post except that we specify the name of the FAST Query Search Service Application Proxy. Now it’s just a matter of creating an instance of the KeywordQuery class and enabling FQL.
KeywordQuery keywordQuery = new KeywordQuery(searchProxy);
keywordQuery.EnableFQL = true;
The rest of the code looks pretty much the same. Let’s start out with an FQL query that looks for an author that end withs the last name of Murphy. To do this, we use the ends-with FQL operator. Our query will look like the following:
author:ends-with(murphy)
This is what the rest of the code looks like. You can see that I used the above query here in the QueryText property.
keywordQuery.QueryText = "author:ends-with(murphy)";
keywordQuery.ResultsProvider = SearchProvider.Default;
keywordQuery.ResultTypes = ResultType.RelevantResults;
ResultTableCollection resultsTableCollection = keywordQuery.Execute();
ResultTable searchResultsTable = resultsTableCollection[ResultType.RelevantResults];
DataTable resultsDataTable = new DataTable();
resultsDataTable.TableName = "Results";
resultsDataTable.Load(searchResultsTable, LoadOption.OverwriteChanges);
I use the Execute() method to perform the query and then the results are loaded into a data table. If you are curious about the details on the rest of those lines of code, see the KeywordQuery post. Using the dataset visualizer, I can see the results of my query.
Pretty simple, right? Now, if I wanted to see all documents that are about termination or beer (obviously the two could be related :) ), we could use the or operator. I’ll just put the new query in the QueryText property and execute the code.
keywordQuery.QueryText = "or(termination, beer)";
Here are the results:
What I really like about this is that you can see what the rank of each result is right there. If you want to start affecting relevance, you should be able to start using the XRANK operator like the example below.
xrank(accounting, budget, boost=500)
This should rank documents with the word budget in them higher. Unfortunately, it has never worked for me though. I’m still looking into it. Perhaps there is a configuration setting I need. As you can see, if you know how to query SharePoint Enterprise Search, it’s not hard to query FAST using FQL either. Remember, that all of your regular KeywordQuery syntax operations work in FAST too. You don’t have to learn all of FQL to query FAST. It’s just there for when you want to perform more advanced queries.