Setting analyzers for Azure Search
Azure Search uses Language analyzers to process text values. By default, searchable fields are analyzed with the Apache Lucene Standard analyzer. Because Azure Search supports a variety of languages, it additionally provides other text analyzers with more advanced capabilities for specific languages.
If you wish to assign a specific analyzer to the fields of an index, customize the functionality that Xperience uses to build Azure Search indexes:
Open your Xperience solution in Visual Studio.
Create a custom module class.
Override the module’s OnInit method and assign a handler to the DocumentFieldCreator.Instance.CreatingField.After event.
Perform the following in the event’s handler method:
Write a condition to limit which search indexes and fields are affected by the analyzer customization (using values of the SearchIndex and SearchField properties of the handler’s CreateFieldEventArgs parameter).
Access the Field property of the handler’s CreateFieldEventArgs parameter, and assign a valid analyzer name into one of its Analyzer properties.
Field analyzer properties
The Field class (Microsoft.Azure.Search.Models.Field) offers the following properties for setting the analyzer:
- Analyzer
- IndexAnalyzer (used at indexing time)
- SearchAnalyzer (used at search time)
For more information, see the Index Attributes section of the Create Index article.
Also deploy the custom module class to the MVC application (otherwise indexing may not work correctly for changes performed through the live site).
Open the administration interface.
Open the Smart search application and Rebuild any related Azure Search indexes.
The customized Azure Search index fields now use the specified language analyzer.
Example
The following example demonstrates how to set the language analyzer for the skudescription field of an Azure Search index named dg-store.
Start by preparing a separate project in your Xperience solution for the custom module class:
- Open your Xperience solution in Visual Studio.
- Add a custom assembly (Class Library project) with class discovery enabled to the solution, or re-use an existing assembly.
- Reference the project from both your live site and Xperience administration (CMSApp) projects.
Continue by implementing the custom module class and rebuilding the related search index:
Create a new class named CustomAzureSearchModule under the SearchCustomization project, with the following code:
using System; using CMS; using CMS.DataEngine; using CMS.Search.Azure; // Registers the custom module into the system [assembly: RegisterModule(typeof(CustomAzureSearchModule))] public class CustomAzureSearchModule : Module { // Module class constructor, the system registers the module under the name "CustomAzureSearch" public CustomAzureSearchModule() : base("CustomAzureSearch") { } // Contains initialization code that is executed when the application starts protected override void OnInit() { base.OnInit(); // Assigns a handler to the CreatingField.After event for Azure Search indexes DocumentFieldCreator.Instance.CreatingField.After += UseCustomSearchAnalyzer; } private void UseCustomSearchAnalyzer(object sender, CreateFieldEventArgs e) { string indexName = e.SearchIndex.IndexCodeName; string fieldName = e.SearchField.FieldName; // Sets the 'en.microsoft' analyzer for the 'skudescription' field in the 'dg-store' index if (indexName.Equals("dg-store", StringComparison.InvariantCultureIgnoreCase) && fieldName.Equals("skudescription", StringComparison.InvariantCultureIgnoreCase)) { e.Field.Analyzer = "en.microsoft"; } } }
Save all changes and Build the SearchCustomization project.
Sign in to the Xperience administration interface.
Open the Smart search application and Rebuild the dg-store index.
The skudescription field of the dg-store index now uses the “en.microsoft” language analyzer, both during indexing and at search time.