DotNet Core Azure Search SDK - filtering results

Viewed 497

We are trying to implement Filter functionality into Azure (Cognitive) Search. I was hoping to find some nice SDK methods that hide all the ugly parts, but so far the only example I found looks like this (source):

SearchParameters parameters = new SearchParameters()
{
    Filter = String.Format("groupIds/any(p:search.in(p, '{0}'))", string.Join(",", groups.Select(g => g.ToString()))),
    Select = new[] { "application essays" }
};

I was wondering, whether I am missing some docs. Or maybe it is on the roadmap?

1 Answers

Check out our new Azure.Search.Documents SDK we released last month. It does have OData filter helps as you can find here:

int stars = 4;
SearchOptions options = new SearchOptions
{
    // Filter to only Rating greater than or equal our preference
    Filter = SearchFilter.Create($"Rating ge {stars}"),
    Size = 5, // Take only 5 results
    OrderBy = { "Rating desc" } // Sort by Rating from high to low
};

It'll escape string parameters correctly. The OData $filter syntax still requires raw input, but the type helpers in the formattable string should make your situation easier: you don't have to worry about escaping values yourself.

Related