Registering custom data types for Azure Search

Azure Search index fields and the fields of objects in Xperience each use a different set of data types. See the Supported data types (Azure Search) article for details.

When building Azure Search indexes, Xperience performs data type mapping and conversions for field values. By default, the following data type mapping is used:

Xperience field data type

C# type

Azure Search data type

Boolean (Yes/No)

bool

Edm.Boolean

Date
Date and time

DateTime

Edm.DateTimeOffset

Decimal number

decimal

Edm.Double

Floating point number (Double precision)

double

Edm.Double

Integer number

int

Edm.Int32

Long integer number

long

Edm.Int64

Long text
Text

string

Edm.String

Time interval

TimeSpan

Not mapped

Unique identifier (GUID)

Guid

Edm.String

If you wish to add values with a custom data type into Azure Search indexes, you first need to register the given data type, and prepare a conversion function that maps the type to one of the types supported by Azure Search. You can also use the same approach if you wish to change how the default Xperience data types map to Azure Search types.

  1. Open your Xperience solution in Visual Studio.
  2. Create a custom module class.
  3. Override the module’s OnInit method and call the RegisterMapping method of the CMS.Search.Azure.DataMapper class. The method’s parameters specify:
    • The original C# data type
    • The corresponding value from the Microsoft.Azure.Search.Models.DataType enumeration
    • A conversion method
  4. Implement the conversion methods used in your RegisterMapping calls.
  5. Also deploy the custom module class to the MVC application (otherwise indexing may not work correctly for changes performed through the live site).

When creating Azure Search indexes, the system now uses the specified data types for the appropriate fields.

Index rebuild required

To apply the custom data types to existing Azure Search indexes:

  1. Sign in to the Xperience administration interface.
  2. Open the Smart search application.
  3. Rebuild any related Azure Search indexes.

Example

The following example demonstrates how to re-map the Decimal number data type of Xperience fields to the Edm.String data type used by Azure Search.

Start by preparing a separate project in your Xperience solution for the custom module class:

  1. Open your Xperience solution in Visual Studio.
  2. Add a custom assembly (Class Library project) with class discovery enabled to the solution, or re-use an existing assembly.
  3. 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 indexes:

  1. Create a new class named CustomAzureSearchModule under the SearchCustomization project, with the following code:

    
    
    
     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();
    
             // Registers custom mapping of the decimal data type in Xperience to strings in Azure Search indexes
             DataMapper.Instance.RegisterMapping(typeof(decimal), Microsoft.Azure.Search.Models.DataType.String, ConvertDecimalToString);
         }
    
         // Converts decimal values to strings
         // The resulting strings use ',' as the group separator, '.' as the decimal separator, and two decimal places
         private string ConvertDecimalToString(object decimalValue)
         {
             return ((decimal)decimalValue).ToString("N2", System.Globalization.CultureInfo.InvariantCulture);
         }
     }
    
    
     
  2. Save all changes and Build the SearchCustomization project.

  3. Sign in to the Xperience administration interface.

  4. Open the Smart search application and Rebuild your Azure indexes.

Any Xperience object fields that use the Decimal number data type are now created as Edm.String type fields within Azure Search indexes.