azureazure-cognitive-searchazure-search-.net-sdk

Azure Search: How to specify search fields in v11


In v10 of Azure Search SDK, I was able to specify exactly which fields to search in an index by taking advantage of SearchParameters class. In v11, I see there is SearchOptions, but the SearchFields parameter is get only. In v10, SearchFields has a setter.

How do I choose which fields to search on in v11?


Solution

  • You can call .Add() on the SearchFields property:

    var options = new SearchOptions();
    options.SearchFields.Add("field1");
    options.SearchFields.Add("field2");
    

    Or you can use C#'s list initializer syntax:

    var options = new SearchOptions() { SearchFields = { "field1", "field2" } };
    

    Example adapted from this GitHub issue.