in

Dot Net Mafia

Group site for Tulsa area .NETdevelopers, with blogs dealing with (usually) .NET, SharePoint, and other Microsoft products, as well as some discussion of general programming related concepts.

This Blog

Syndication

Corey Roth - DotNetMafia.com - Tip of the Day

Bringing you the latest time saving tips for SharePoint, MOSS 2007, ASP.NET, LINQ, and Visual Studio 2008

How to: Use the MOSS Enterprise Search KeywordQuery class

I don't think there are enough complete examples on using the KeywordQuery class out there, so I am posting this today to help out.  The example in the SDK is close, but not quite enough.  The KeywordQuery class is used to execute a keyword syntax query against MOSS Enterprise Search.  There is also a similar class that uses WSS search which basically works the same way.  To use the KeywordQuery class start by passing it the path to your SSP in the constructor.  This is a good place to use object initializers as I pointed out last week to set other needed properties.

// create a new KeywordQuery class, set the query and set to RelevantResults.

KeywordQuery myQuery = new KeywordQuery(siteCollection)

{

    QueryText = string.Format("Color:\"{0}\"", "Red"),

    ResultTypes = ResultType.RelevantResults

};

In this case I am doing a keyword query searching on the managed property Color with a value of red.  You must set the ResultTypes property to RelevantResults in order to get search results back.  To execute the query, use the Execute method.  This method returns a ResultTableCollection which in turn contains a ResultTable for each type of Result (i.e.: RelevantResults).  You can then load this into a datatable and do whatever with the data.

// execute the query and load the results into a datatable

ResultTableCollection queryResults = myQuery.Execute();

ResultTable queryResultsTable = queryResults[ResultType.RelevantResults];

DataTable queryDataTable = new DataTable();

queryDataTable.Load(queryResultsTable, LoadOption.OverwriteChanges);

Out of the box, this code will only return the default search properties such as Rank, Title, Author, Size, Path, Description, etc.  If you want to return managed properties, make use of the SelectProperties string collection.

myQuery.SelectProperties.Add("Color");

myQuery.SelectProperties.Add("Size");

myQuery.SelectProperties.Add("Quantity");

Whenever I have worked with this class, I have discovered that adding anything to the SelectProperties collection will cause the default properties to no longer be returned.  For example add the title and path properties back to the results with the following.

myQuery.SelectProperties.Add("Title");

myQuery.SelectProperties.Add("Path");

Lastly, using LINQ to DataSet you can further subquery your results if you needed to (i.e.: with a Quantity > 10).

var results = from queryResult in queryDataTable.AsEnumerable()

              where queryResult.Field<int>("Quantity") > 10

              select new

              {

                  Title = queryResult.Field<string>("Title"),

                  Path = queryResult.Field<string>("Path"),

                  Size = queryResult.Field<string>("Size"),

                  Quantity = queryResult.Field<int>("Quantity")

              };

Here is the complete code sample.

using (SPSite siteCollection = new SPSite(siteCollectionUrl))

{

    // create a new KeywordQuery class, set the query and set to RelevantResults.

    KeywordQuery myQuery = new KeywordQuery(siteCollection)

    {

        QueryText = string.Format("Color:\"{0}\"", "Red"),

        ResultTypes = ResultType.RelevantResults

    };

 

    //

    myQuery.SelectProperties.Add("Title");

    myQuery.SelectProperties.Add("Path");

    myQuery.SelectProperties.Add("Color");

    myQuery.SelectProperties.Add("Size");

    myQuery.SelectProperties.Add("Quantity");

 

    // execute the query and load the results into a datatable

    ResultTableCollection queryResults = myQuery.Execute();

    ResultTable queryResultsTable = queryResults[ResultType.RelevantResults];

    DataTable queryDataTable = new DataTable();

    queryDataTable.Load(queryResultsTable, LoadOption.OverwriteChanges);

 

    // query the results into a new anonymous type

    var results = from queryResult in queryDataTable.AsEnumerable()

                  where queryResult.Field<int>("Quantity") > 10

                  select new

                  {

                      Title = queryResult.Field<string>("Title"),

                      Path = queryResult.Field<string>("Path"),

                      Size = queryResult.Field<string>("Size"),

                      Quantity = queryResult.Field<int>("Quantity")

                  };

}

Comments

No Comments

About CoreyRoth

Corey Roth is a MOSS Consultant for Stonebridge, Inc. specializing in clients in the Energy Sector.
2009 dotnetmafia.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems