---
title: "Walkthrough: Create a custom index field handler"
slug: "walkthrough--create-a-custom-index-field-handler"
abstract: "How to create a custom index field handler class, when the default Search plugin does not provide a handler type that suits a new Catalog entity you want to include in the Commerce search index."
---
# Walkthrough: Create a custom index field handler

You can create a custom field handler class, when the default Search plugin does not provide a handler type that suits the new field you want to add to the index. This walkthrough describes how to:

- Create a new field handler to index a new field.
- Add a new field to CE Connect search provider configuration to make it searchable in Sitecore.

## Create a custom field handler to index a new field

To create a custom field handler:

> [!note]
> The current architecture of search field handlers does not provide the ability to resolve an instance of a pipeline or command. You must add values that the handler requires to the context.

1. Create a class that inherits from the [Sitecore.Commerce.Plugin.Search.AbstractIndexFieldHandler](https://doc.sitecore.com/xp/en/developers/103/sitecore-experience-commerce/the-sitecore-commerce-plugin-search-abstractindexfieldhandler-class.html) class, and implements the `ComposeValue` method, as shown in the following example:

    ```csharp
    public class CustomFieldHandler : AbstractIndexFieldHandler
    {
      /// <inheritdoc />
       public override object ComposeValue(object source, ConcurrentDictionary<string, object> context)
       {
    	if (context == null || source == null || !(source is CatalogItemBase entity))
    	{
    	   return null;
    	}
    	return $"Custom field handler value: {entity.Id}";
       }
    }
    ```

2. To register the custom field handler with the search index, you must add it to the search policy set for the search provider used in your deployment. The policy sets for Azure Search (`PlugIn.Search.Azure.PolicySet-1.0.0.json`) and Solr (`PlugIn.Search.Solr.PolicySet-1.0.0.json`) each contain two policies of the type `Sitecore.Commerce.Plugin.Search.ItemIndexablePolicy, Sitecore.Commerce.Plugin.Search`. These policies represent the Master and Web databases of the connected Sitecore XP site. You add your custom field handler in the `“Fields”` collection for both indexes to register the new handler and ensure that your content is indexed in both the Master and the Web databases.

    The following shows an example of a custom Solr field handler (`"Company.Project.Search.CustomFieldHandler, Company.Project"`) added to the `PlugIn.Search.Solr.PolicySet-1.0.0.json` file.

    ```csharp
    {
        "$type": "Sitecore.Commerce.Plugin.Search.ItemIndexablePolicy,
    Sitecore.Commerce.Plugin.Search",
        "IndexName": "sitecore_web_index",
        "FieldTypeMappers": [
            …
        ],
        "Fields": [
           {
               "$type":
    "Sitecore.Commerce.Plugin.Search.Solr.SolrIndexFieldConfiguration,
    Sitecore.Commerce.Plugin.Search.Solr",
               "Name":
    "customfield",
               "Type":
    "System.String",
               "Handler": {
                   "$type": "Company.Project.Search.CustomFieldHandler, Company.Project"
                }
            }
        ]
    }
    ```

    The following shows an example of a custom Azure Search field handler (`"Company.Project.Search.CustomFieldHandler, Company.Project"`) added to the `PlugIn.Search.Azure.PolicySet-1.0.0.json` file.

    ```csharp
    {
        "$type":
    "Sitecore.Commerce.Plugin.Search.ItemIndexablePolicy,
    Sitecore.Commerce.Plugin.Search",
        "IndexName": "sitecore_web_index",
        "FieldTypeMappers": [
            …
        ],
        "Fields": [
            {
               "$type": "Sitecore.Commerce.Plugin.Search.Azure.AzureIndexFieldConfiguration,
    Sitecore.Commerce.Plugin.Search.Azure",
               "Name":
    "customfield",
               "Type": "System.String",
               "Handler": {
                   "$type": "Company.Project.Search.CustomFieldHandler, Company.Project"
                }
            }
        ]
    }
    ```

3. After the configuration changes, [bootstrap the Commerce Engine environment](https://doc.sitecore.com/xp/en/developers/100/sitecore-experience-commerce/bootstrap-the-commerce-engine.html "Bootstrap the Commerce Engine") to apply the changes.
4. In Microsoft IIS Manager, restart the Commerce Engine minions service (*CommerceMinions_Sc*).
5. [Manually rebuild Sitecore XP indexes](https://doc.sitecore.com/xp/en/developers/100/sitecore-experience-commerce/manually-rebuild-sitecore-xp-indexes.html "Manually rebuild Sitecore XP indexes"). After rebuilding the indexes, the new search handlers are visible in the search index, as shown in the following example:

    ![Search handlers in the search index.](https://doc.sitecore.com/api/markdown-image/xp/en/developers/100/sitecore-experience-commerce/images/XC_Dev_Ref_Index_with_custom_search_handler.png)

## Add a new field to CE Connect search provider configuration

In order to make the new field searchable in Sitecore, you must add it to the search provider configuration in CE Connect.

To add the new field to the search provider configuration in CE Connect:

1. On your Sitecore service instance, in the `\App_Config\Include\Y.Commerce.Engine` folder, open the search provider config file. For example, for Sorl, open the `Sitecore.Commerce.Engine.Connectors.Index.Solr.config` file.

    > [!note]
    > If you use Azure as your search provider, open the `Sitecore.Commerce.Engine.Connectors.Index.Azure.config` file.

2. In the `FieldMap` section, add the new `fieldName` definition. In the following example, we add a new field named `"exampleexternalsettingproperty"`.

    ```text
    <field fieldName="exampleexternalsettingproperty" storageType="YES" indexType="UN_TOKENIZED" vectorType="NO" boost="1f" returnType="string" settingType="Sitecore.ContentSearch.SolrProvider.SolrSearchFieldConfiguration, Sitecore.ContentSearch.SolrProvider"/>
    ```

3. Save your changes and [bootstrap the Commerce Engine](https://doc.sitecore.com/xp/en/developers/100/sitecore-experience-commerce/bootstrap-the-commerce-engine.html "Bootstrap the Commerce Engine").