2

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.

enter image description here

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.

1 Answer 1

6

Your filters should be:

var refinementFilters = new string[] { "doc, docx" };
for (var i = 0; i < refinementFilters.Length; i++)
{
   keywordQuery.RefinementFilters.Add("FileType:" + refinementFilters[i]);
}

where you add the property you want to filter on as part of the filter, not just the value. The above sample might not be the best as there is an AND between doc and docx. If you want OR then follow the sample below using FQL.

A good exercise is to use fiddler or similar when you click a refiner in the UI, and see what the query xml looks like upon submission. When clicking the "Word" refiner this filter is added to the query:

FileType:or(equals("docx"),equals("doc"),equals("docm"),equals("dot"),equals("nws"),equals("dotx"))

As you can see this is a FQL formatted query, and you can create something similar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.