Search code examples
c#.netazure.net-coreazure-cognitive-search

How to use Azure Search SDK to filter on specific column


I am using Azure Search and I want to make a simple filter expression using Azure Search SDK, however I cant find documentation in how to do it by code when I want to filter on a specific field.

This work on the Search explorer

enter image description here

However, in Code, this doesnt work;

I get a syntax error:

Console.Write("Apply a filter to the index to find roles  with a rolename:Usuario, ");
            Console.WriteLine("and return the id and name:\n");

            parameters =
                new SearchParameters()
                {
                    Filter = "RoleName:Partner",
                    Select = new[] { "id", "RoleName" }
                };

            results = indexClient.Documents.Search<Role>("*", parameters);

            WriteDocuments(results);

The code is based on this sample:

https://github.com/Azure-Samples/search-dotnet-getting-started/blob/master/DotNetHowTo/DotNetHowTo/Program.cs


Solution

  • You can try

    new SearchParameters()
        {
            Filter = "RoleName eq 'Partner'",
            Select = new[] { "id", "RoleName" }
        };
    

    Generally speaking, since the SDK is open source, you can use the unit tests on GitHub to find examples on how to use some of the more common features.