Mike
Mike

Reputation: 205

How to define CustomAnalyzer through C# code when creating Azure Search Index

I saw the note from here: https://learn.microsoft.com/en-us/rest/api/searchservice/custom-analyzers-in-azure-search that: Custom analyzers that you create are not exposed in the Azure portal. The only way to add a custom analyzer is through code that makes calls to the API when defining an index.

Currently I am using the following C# code to create the Azure search index:

var definition = new Microsoft.Azure.Search.Models.Index()
{
    Name = "test-index",
    Fields = new List<Field>
    {
        new Field("field1", Microsoft.Azure.Search.Models.DataType.String) { IsKey = false, IsSearchable = false, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true }
    }
}
searchClient.Indexes.CreateOrUpdate(myIndex);

Is there a way that I can add some code in here such as:

var analyzer = new CustomAnalyzer();
analyzer.Tokenizer = TokenizerName.Keyword;
analyzer.TokenFilters.Add(TokenFilterName.Lowercase);

and then add this custom analyzer in my code above somewhere to let "field1" use my defined analyzer?

Upvotes: 1

Views: 930

Answers (1)

Yahnoosh
Yahnoosh

Reputation: 1972

Yes, you can add custom analyzers through the SDK, here are some examples: https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/search/Microsoft.Azure.Search/tests/Tests/CustomAnalyzerTests.cs

Upvotes: 1

Related Questions