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.

YodasMyDad
  • 9,248
  • 24
  • 76
  • 121

1 Answers1

1

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.

Bruce Johnston
  • 8,344
  • 3
  • 32
  • 42
Yahnoosh
  • 1,932
  • 1
  • 11
  • 13
  • Sorry that's incorrect. " Once the analyzer is chosen, it cannot be changed for the field" that is from the Microsoft website. – YodasMyDad Jun 13 '17 at 05:56
  • Our documentation explains that the analyzer can't be changed on an existing field. From you question I understand, that you want to change the analyzer on a field of an index that you're programatically building but doesn't exist in the service yet. Is this not true? – Yahnoosh Jun 13 '17 at 05:59
  • As per the question. I want to use the same class but just change the Analyzer based on user input before the index is created. – YodasMyDad Jun 13 '17 at 15:30
  • You can do it by setting the analyzer property on the field. – Yahnoosh Jun 13 '17 at 15:52
  • Can you give me an example based on my question above please? – YodasMyDad Jun 14 '17 at 06:12
  • Thank you Bruce. Appreciate your time writing this answer. – YodasMyDad Jun 15 '17 at 05:49