Naming things is hard, naming index field is scarier

Friends, Sitecorians, country(wo)men, lend me your ears!


I hope you all enjoyed the Sitecore Symposium, and for those who missed it – It’s not too late. All sessions are there, ready for you to watch and be amazed. 🙂


Before we start, I would like to give a shout out to all you Sitecore Meetups organizers who are doing online events. Please, keep doing this (In-Person and online event)

Last night(for me), it was the Sitecore Usergroup Brazil, with the topic – React for Sitecore Dinosaurs + Content Hub Migration. Great work. Keep those incredible and lovely online events coming.  🙂

Today’s post will be about a strange “index error” that appeared out of nowhere, and it was tough to find out why it happened. Scary stuff  🎃

Let me give you a background to it:
In Sitecore Commerce, we have articles/products(imported from an external system). They have a bunch of attributes, and we want them to be searchable “in Sitecore.” So to make it happen, we’ve added the “attribute fields” to the PlugIn.Search.Solr.PolicySet-1.0.0.json file, something like this:

{
	"$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": "SomeFieldShowingDecimalData",
		  "Type": "System.Decimal",
		  "Handler": {
			"$type": "Sandbox.Commerce.Plugin.MyPlugin.IndexFields.MyArticleComponentHandler, Sandbox.Commerce.Plugin.MyPlugin",
			"ComposerSource": {
			  "ViewName": "MyArticleComponent",
			  "PropertyName": "SomeFieldShowingDecimalData"
			}
		  }
		},
		...
	]
}

We created a custom Index field handler – MyArticleComponentHandler:

namespace Sandbox.Commerce.Plugin.MyPlugin.IndexFields
{
    public class MyArticleComponentHandler: AbstractIndexFieldHandler
    {
        public ComposerValueSource ComposerSource { get; set; } = new ComposerValueSource(string.Empty, string.Empty);


        public override object ComposeValue(object source, ConcurrentDictionary<string, object> context)
        {
            if (context == null || !(source is CatalogItemBase catalogItemBase))
                return null;

            if (!(catalogItemBase is SellableItem))
                return null;

            var sellableItem = catalogItemBase as SellableItem;


            if (catalogItemBase.EntityComponents.All(component => component.GetType().Name != ComposerSource.ViewName))
                return null;

            if (ComposerSource.ViewName != "MyArticleComponent")
                return null;

            var component = sellableItem.GetComponent<MyArticleComponent>();
            if (component == null)
                return null;

            return ComposerSource.PropertyName switch
            {
                "SomeFieldShowingDecimalData" => component.AnAttributeField,
                "SomeOtherFieldShowingText" => component.SomeOtherAttributeField,
                _ => null
            };
        }


    }
}

The last part was to add the new fieldnames(in Sitecore) in a patched config file:

<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"
               xmlns:role="http://www.sitecore.net/xmlconfig/role/"
               xmlns:search="http://www.sitecore.net/xmlconfig/search/">
  <sitecore search:require="solr">
    <contentSearch>
      <indexConfigurations>
          <defaultSolrIndexConfiguration>
            <fieldMap type="Sitecore.ContentSearch.SolrProvider.SolrFieldMap, Sitecore.ContentSearch.SolrProvider">
              <fieldNames hint="raw:AddFieldByFieldName">
                  ...		
                  <field fieldName="somefieldshowingdecimaldata" storageType="YES" indexType="TOKENIZED" vectorType="NO" boost="1f" returnType="double" settingType="Sitecore.ContentSearch.SolrProvider.SolrSearchFieldConfiguration, Sitecore.ContentSearch.SolrProvider" patch:source="Sitecore.Commerce.Engine.Connectors.Index.Solr.config"/>
                  <field fieldName="someotherfieldshowingtext" storageType="YES" indexType="UN_TOKENIZED" vectorType="NO" boost="1f" returnType="string" settingType="Sitecore.ContentSearch.SolrProvider.SolrSearchFieldConfiguration, Sitecore.ContentSearch.SolrProvider" patch:source="Sitecore.Commerce.Engine.Connectors.Index.Solr.config"/>
                 ...
              </fieldNames>
			...

It worked great and the new fields(and values) were successfully added to the Sitecore index(web).

It ran smoothly for some time(months), but suddenly indexing errors started to emerge, something like this:

Exception: SolrNet.Exceptions.SolrConnectionException
Message: <?xml version="1.0" encoding="UTF-8"?>
<response>

<lst name="responseHeader">
  <int name="rf">1</int>
  <int name="status">400</int>
  <int name="QTime">1</int>
</lst>
<lst name="error">
  <lst name="metadata">
    <str name="error-class">org.apache.solr.common.SolrException</str>
    <str name="root-error-class">java.lang.NumberFormatException</str>
  </lst>
  <str name="msg">ERROR: [doc=sitecore://master/{bdf069e5-00fa-4bd6-a8c3-85c743ef0e44}?lang=sv-se&amp;ver=1&amp;ndx=sitecore_web_index] Error adding field 'somefieldshowingdecimaldata_td'='Some text' msg=For input string: "Some text"</str>
  <int name="code">400</int>
</lst>
</response>

Source: SolrNet
   at SolrNet.Impl.SolrConnection.PostStream(String relativeUrl, String contentType, Stream content, IEnumerable`1 parameters)
   at SolrNet.Impl.SolrConnection.Post(String relativeUrl, String s)
   at SolrNet.Impl.LowLevelSolrServer.SendAndParseHeader(ISolrCommand cmd)
   at Sitecore.ContentSearch.SolrProvider.SolrBatchUpdateContext.Commit()
   at Sitecore.ContentSearch.AbstractSearchIndex.PerformUpdate(IEnumerable`1 indexableInfo, IndexingOptions indexingOptions)

My first thought was, what has gone wrong with the import? Why do they set string data on a decimal field? So I started going through the import job and did all kinds of fail-safe coding in the MyArticleComponentHandler. Still the same error. This was a nightmare.

Then it struck me, what if someone has created a “normal string” field in Sitecore? And guess what? That was the reason. Someone had created a field(to a datasource template) with the same name as we were using in Sitecore Commerce. To fix it was easy, I just renamed the index fields in Sitecore Commerce, something like this – SomeFieldShowingDecimalDataCommerce

Lessons learned: Be careful when creating and naming your index fields, especially if it’s in “another” system.

That’s all for now folks 😊


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.