Sharepoint 2013 Keywordquery search returns less results then default sharepoint search

I had a requirement in which I want to get the search results programmatically. For this I am using KeyWordQuery to search programmatically. But I am getting less results than default SharePoint search, it is omitting some records (duplicate records) from the results. After spending some time I found one property TrimDuplicates of KeywordQuery object. Set this property to false for fetch all search data.

 

SPSite site = SPContext.Current.Site;
KeywordQuery objkeywordquery = new KeywordQuery(site);
objkeywordquery.QueryText = "Your query goes here";
objkeywordquery.SelectProperties.Add("Field1");
objkeywordquery.SelectProperties.Add("Field2");
objkeywordquery.ResultsProvider = Microsoft.Office.Server.Search.Query.SearchProvider.Default;
objkeyworkquery.TrimDuplicates = false;
ResultTableCollection queryResults = objkeywordquery.Execute();
ResultTable queryResultsTable = queryResults[ResultType.RelevantResults];
DataTable queryDataTable = new DataTable();
queryDataTable.Load(queryResultsTable, LoadOption.OverwriteChanges);

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*
Website