Question

I am trying to retrieve the search result from specific Sub Site using KQL in SharePoint 2010.but I am getting only one result with custom code. However the OOTB Search result returns 5 results for the same Query Text.

I have already checked This Link

Here is my Code .

using (SPSite scSite = new SPSite(SPContext.Current.Web.Url)) 
        {
          using (SPWeb webRoot = scSite.OpenWeb()) 
          {
             scSite.AllowUnsafeUpdates = true;
             webRoot.AllowUnsafeUpdates = true;
             SearchServiceApplicationProxy proxy = (SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy(SPServiceContext.GetContext(SPContext.Current.Site));
             KeywordQuery keywordQuery = new KeywordQuery(proxy);
             keywordQuery.ResultsProvider = Microsoft.Office.Server.Search.Query.SearchProvider.Default;
             keywordQuery.TrimDuplicates = false;
             keywordQuery.EnableStemming = true;
             keywordQuery.QueryText = query;
             keywordQuery.HiddenConstraints = "This Site: " + SPContext.Current.Web.Name + "";
             keywordQuery.ResultTypes = ResultType.RelevantResults;
             ResultTableCollection searchResultTables = keywordQuery.Execute();
             ResultTable searchResult = searchResultTables[ResultType.RelevantResults];
             var resultsDataTable = new DataTable {TableName = "Results"};
             resultsDataTable.Load(searchResult, LoadOption.OverwriteChanges);
           }
        }
Was it helpful?

Solution

I solved the issue by using the Path managed property in the QueryText of KQL.

i.e., query = (Search Term) ((Path:a))

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top