While working on a project using Sitecore commerce 9.3, we came across a requirement that requires...
Sitecore Commerce – Add new field to Solr Index
When an index rebuild is invoked, sitecore XC indexes the properties of the catalog entity by default. These indexed fields are added to web & master indexes. To add a custom field to the index, we need to map the custom field to a field type & create a custom field handler. To register the new custom field, we need to add an entry in the fields with appropriate type & add it to the search policy set. In this case, “PlugIn.Search.Solr.PolicySet-1.0.0.json”.
"$type" : "Sitecore.Commerce.Core.PolicySet, Sitecore.Commerce.Core" ,
"Id" : "Entity-PolicySet-SolrSearchPolicySet" ,
"Version" : 1 ,
"IsPersisted" : false ,
"Name" : "SolrSearchPolicySet" ,
"Policies" : {
"$type" : "System.Collections.Generic.List`1[[Sitecore.Commerce.Core.Policy, Sitecore.Commerce.Core]], mscorlib" ,
"$values" : [
{
"$type" : "Sitecore.Commerce.Plugin.Search.ItemIndexablePolicy, Sitecore.Commerce.Plugin.Search" ,
"IndexName" : "sitecore_web_index" ,
"FieldTypeMappers" : [
{
"TypeName" : "string" ,
"Type" : "System.String" ,
"NameFormat" : "{0}_s" ,
"CultureFormat" : "_{1}"
}
] ,
"Fields" : [
{
"$type" : "Sitecore.Commerce.Plugin.Search.Solr.SolrIndexFieldConfiguration, Sitecore.Commerce.Plugin.Search.Solr" ,
"Name" : "CustomField" ,
"Type" : "System.String" ,
"TypeHint" : "string" ,
"Handler" : {
"$type" : "Custom.Sitecore.Commerce.Plugin.InRiverConnector.IndexFieldHandlers.CustomFieldIndexFieldHandler,
Custom.Sitecore.Commerce.Plugin.InRiverConnector"
}
}
]
}
]
}
}
We need to add a custom field handler to compose a value for the custom field that needs to be indexed. To create a custom field index handler, we need to create a class that inherits from the “Sitecore.Commerce.Plugin.Search.AbstractIndexFieldHandler class”, and implement the ComposeValue method.
{
public override object ComposeValue ( object source, ConcurrentDictionary < string, object > context )
{
if (context == null || source == null )
{
return null ;
}
return "Sample custom field handler value" ;
}
}
We need to bootstrap the commerce engine environment to apply the changes. After this we need to restart the commerce engineer & rebuild the indexes.