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.
Once your Index instance is created from a model class, you can access the list of Fields and change their properties, the Analyzer is one of them.
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.
Alternatively, you can just build the Field
instances yourself:
var index = new Index()
{
Name = "myindex",
Fields = new List<Field>()
{
new Field("Title", DataType.String, "fr.microsoft"),
new Field("Description", DataType.String, "fr.microsoft")
}
}
In both cases you can use a string for the analyzer name, which you could receive as user input or from config.