Friday, August 14, 2015

SharePoint 2013 Search query doesn't return all results

Sometimes the search query doesn't return all search result when querying search index using KQL and KeywordQuery. This happens because query component considers some result entries as duplicates. To prevent this behavior, a small query tuning should be applied:

using (KeywordQuery query = new KeywordQuery(searchApplicationProxy))
{
    query.SelectProperties.Add("put managedproperty name here");

    query.QueryText = "put your query text here";
    query.TrimDuplicates = false;
    SearchExecutor se = new SearchExecutor();
    ResultTableCollection resultTableCollection = se.ExecuteQuery(query);
}


The key is to set TrimDuplicates property to false for KeywordQuery component.