top of page
  • Hina Garg

Indexing custom fields added to a Catalog - Sitecore Experience Commerce 9.3

Updated: Sep 1, 2020

Hello Everyone,

Hope you all are doing great! I am very excited to share with you all my findings on this piece of functionality. As the title of this post indicates, I am going to describe how to index a property field added to the Catalog entity properties in Commerce Engine.

When working on real time projects we often come across the need of creating our own catalog and product schema. The schema consists of several property fields added to it as per the requirements of our project. Once the catalog and product schema are ready, we import them in Sitecore XP. The next thing that comes to our mind is to query the Sitecore content tree and fetch different products, categories, variants exposed by the Catalog on our storefront pages. Although there are few OOTB search functions provided by Sitecore Commerce XA, we might also need to write our own search functions.


The custom search functions would involve querying the products under Catalog on XP side of Sitecore. The success of a query would depend on whether or not the custom properties or fields that are getting used in the query are indexed. And to our surprise we might notice that the custom fields that we added in the product are not getting indexed although <indexallfields> is set to true for our index. This is because the Commerce Crawler traverses the catalog content stored in the Commerce Engine instead of traversing the catalog content exposed in the Sitecore content tree by the data provider. So, we need to map our custom property field in a search index. We can achieve this mapping through configuration in a policy set file which is PlugIn.Search.Solr.PolicySet.*.json. For this field mapping to complete we need a field handler, if a default search field handler does not exist for our field then we need to define a custom index field handler. I had to create a custom field handler in my case and it worked.



To create a custom field handler:

1. Create a class that inherits from the Sitecore.Commerce.Plugin.Search.AbstractIndexFieldHandler class, and implements the ComposeValue method, as shown in the following example:

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, we must add it to the search policy set for the search provider used in our deployment. The policy set that I am using is Solr (PlugIn.Search.Solr.PolicySet-1.0.0.json) it contains two policies of the type Sitecore.Commerce.Plugin.Search.ItemIndexablePolicy, Sitecore.Commerce.Plugin.Search.

I added the custom field handler to both Master and 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.

{   
    "$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"
            }
        }
    ]
}

3. After the configuration changes,bootstrap the Sitecore Commerce Engine environment to apply the changes.

4. Restart Commerce Engine minion service

5. Build index in Sitecore XP. The new search handler will be visible.


Thanks for taking out some time to read this blog.



180 views0 comments
bottom of page