Using KeywordQuery to search in your SharePoint site

There are various ways that you can query the Search Service in your SharePoint site:

1) Using the Search Web Service

2) Using the SharePoint Object Model

There are yet again two ways you can use the Search, using the Object Model:

i) KeywordQuery

ii) FullTextSqlQuery

I think it is a good option to use Object Model when you want to customize the Search (mostly in your WCM websites), and KeywordQuery comes in very handy!

Using KeywordQuery to search is pretty simple! (provided you have set up your Search properly)

KeywordQuery keywordQuery = new KeywordQuery(SPContext.Current.Site);
keywordQuery.ResultTypes = ResultType.RelevantResults | ResultType.SpecialTermResults;
keywordQuery.StartRow = 1;
keywordQuery.RowLimit = 5;
keywordQuery.SortList.Add("Rank", SortDirection.Descending);
keywordQuery.EnableStemming = true;
keywordQuery.TrimDuplicates = true;
keywordQuery.IgnoreAllNoiseQuery = true;
keywordQuery.HiddenConstraints = "scope:" + "\"" + SearchScope + "\"";
keywordQuery.QueryText = SearchText;
ResultTableCollection searchResults = keywordQuery.Execute();

As you can see above, I have combined the result type to be both RelevantResults and SpecialTermResults

Note: In WSS, only RelevantResults will work

One other thing to note in the above code is how we specify the scope using the HiddenConstraints

The search results are returned as a table collection. You can access the search tables as follows:

ResultTable relevantResultsTable = searchResults[ResultType.RelevantResults];
ResultTable specialTermResultsTable = searchResults[ResultType.SpecialTermResults];

If you want to load these into a DataSet with tables RelevantResults and SpecialTermResults respectively, then:

// Relevant Results table
DataTable relevantResults = new DataTable();
relevantResults.TableName = "RelevantResults";
 
// SpecialTerms Results table
DataTable specialTermResults = new DataTable();
specialTermResults.TableName = "SpecialTermResults";
 
// Search Results DataSet
DataSet resultSet = new DataSet();
 
 // Relevant Results
relevantResults.Load(relevantResultsTable, LoadOption.OverwriteChanges);
resultSet.Tables.Add(relevantResults);
 
// SpecialTerms Results
resultSet.Tables.Add(specialTermResults);
specialTermResults.Load(specialResultsTable, LoadOption.OverwriteChanges);

You can now easily databind this DataSet

 

 

If you have worked with the Search Web Service, you will notice that the properties in KeywordQuery and the Search Query XML are similar

<?xml version="1.0" encoding="utf-8" ?>
<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="STRING" >  SCOPE:"All Sites"</QueryText>
 </Context>
<SortByProperties><SortByProperty name="Rank" direction="Descending" order="1"/></SortByProperties>
 <Range><StartAt>1</StartAt><Count>5</Count></Range>
 <EnableStemming>true</EnableStemming>
 <TrimDuplicates>true</TrimDuplicates>
 <IgnoreAllNoiseQuery>true</IgnoreAllNoiseQuery>
 <ImplicitAndBehavior>true</ImplicitAndBehavior>
 <IncludeRelevanceResults>true</IncludeRelevanceResults>
 <IncludeSpecialTermResults>true</IncludeSpecialTermResults>
 <IncludeHighConfidenceResults>true</IncludeHighConfidenceResults>
</Query></QueryPacket>

(Above Search Query XML was generated using SharePoint Search Service Tool)


Comments

pingback

Pingback from murratore.wordpress.com

Daily Blog Post 10/20/2009 « Murratore’s Weblog

Jun 16 Himani

Himani

Hi,

Can we also specify other managed properties in HiddenConstraints? Suppose I want to search for keywords in a particular scope where color=red.

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading





Creative Commons License
Chaks' Corner Blog by Chakkaradeep Chandran is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Based on a work at www.chakkaradeep.com.
Permissions beyond the scope of this license may be available at http://www.chakkaradeep.com.