0

I am setting up my Azure Search index using the API/SDK attributes. But I want to be able to change the Analyzer for a specific index based on an app setting (i.e. User sets language to French, so this index will use the French Analyzer).

Example of a couple of my index properties

    [IsSearchable]
    [Analyzer(AnalyzerName.AsString.EnMicrosoft)]
    public string Title { get; set; }

    [IsSearchable]
    [Analyzer(AnalyzerName.AsString.EnMicrosoft)]
    public string Description { get; set; }

I am setting the Analyzer to the Microsoft English one. But let's say I want to create another index, but this time using the Microsoft French Analyzer.

Is there a way to programmatically set this, apart from using an attribute? Some sort of event? OnIndexCreating etc... As it's restricting for more complex apps.

I can't have a separate field for each language either as I don't know what languages the user might choose.

Any help appreciated.

4

1 回答 1

1

从模型类创建索引实例后,您可以访问字段列表并更改其属性分析器就是其中之一。

var index = new Index()
{
    Name = "myindex",
    Fields = FieldBuilder.BuildForType<MyModel>()
};

Field field = index.Fields.First(f => f.Name == "Title");
field.Analyzer = "fr.microsoft"; // There is an implicit conversion from string to AnalyzerName.

或者,您可以Field自己构建实例:

var index = new Index()
{
    Name = "myindex",
    Fields = new List<Field>()
    {
        new Field("Title", DataType.String, "fr.microsoft"),
        new Field("Description", DataType.String, "fr.microsoft")
    }
}

在这两种情况下,您都可以使用字符串作为分析器名称,您可以将其作为用户输入或从配置中接收。

于 2017-06-12T16:23:10.357 回答