I m working on requirement where I need to write a client side managed code in C# to pull search results based on the free text query and then should be able to refine the result based on the Keyword Query refiners and refinement filters. So far free text query is working fine but I m not able to pass the refiners and refinement filters to get specific search results. Below I have put my code that I m working on.
string _url = "http://hostname/sites/sitename";
string _queryText = "test";
using (ClientContext clientContext = new ClientContext(_url))
{
var keywordQuery = new KeywordQuery(clientContext)
{
QueryText = _queryText
,Refiners = "FileType"
};
var refinementFilters = new string[] { "doc, docx" };
for (var i = 0; i < refinementFilters.Length; i++)
{
keywordQuery.RefinementFilters.Add(refinementFilters[i]);
}
SearchExecutor searchExecutor = new SearchExecutor(clientContext);
ClientResult<ResultTableCollection> results = searchExecutor.ExecuteQuery(keywordQuery);
clientContext.ExecuteQuery();
Console.WriteLine("result..");
Console.WriteLine("result count: {0}", results.Value[0].ResultRows.Count());
foreach (var resultRow in results.Value[0].ResultRows)
{
Console.WriteLine("{0}: {1}", resultRow["Title"], resultRow["Path"]);
}
Console.WriteLine("Press enter to exit...");
Console.ReadLine();
}
When I execute the above code I get no results, but I have verified that there are results for the filter conditions from the search center page.
Is there some mistake in the way I m trying to use the KeywordQuery.Refiners and KeywordQuery.RefinementFilers?
PS - If I pass "test filetype:pdf" as Query text it returns the expected result as in the search result page. But I was hoping to use the KeywordQuery refiner properties to do the same.
UPDATE -
Following Mikael's answer I was also able to figure out how to pull the refiner details from the search result.
First step is to let the search engine know what refiners you like to get
keywordQuery.Refiners = "FileType";
then on execution of search, check
results.Value[1].ResultRows
for the refiner details.
Note its the second results table that holds the refiner details.