Azure 搜索 API/SDK 分析器属性替代

Azure Search API/SDK Analyzer Attribute Alternative

我正在使用 API/SDK 属性设置我的 Azure 搜索索引。但我希望能够根据应用程序设置更改特定索引的分析器(即用户将语言设置为法语,因此该索引将使用法语分析器)。

我的几个索引属性的示例

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

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

我正在将分析器设置为 Microsoft 英语版。但是假设我想创建另一个索引,但这次使用的是 Microsoft French Analyzer。

除了使用属性之外,有没有办法以编程方式设置它?某种事件? OnIndexCreating 等...因为它限制了更复杂的应用程序。

我也无法为每种语言设置单独的字段,因为我不知道用户可能会选择哪些语言。

感谢任何帮助。

一旦您的 Index instance is created from a model class, you can access the list of Fields and change their properties, the Analyzer 成为其中之一。

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")
    }
}

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