Sitecore Custom Index Computed Field

Xing Liu • 2/19/2016

In our previous Sitecore application, we sometimes need to have a custom computed index field based on the business requirements.  For example, we might need an extra field for faceting purpose of certain data template. In order to have a custom index field, you need to create a custom class that inherits from the AbstractComputedIndexField.

The class and function should look like the following:

public class CustomField : AbstractComputedIndexField
{
        public override object ComputeFieldValue(IIndexable indexable)
        {
            object ret = null;
            //Code to generate the field content and assign to the return object.
            return ret;
        }
}

In your custom config file, make sure you have the following section pointing to your custom class and namespace:

<fields hint="raw:AddComputedIndexField">
	<field fieldName="customFieldName">CAGovPortal.Custom.Index.ComputedFields.CustomField, CAGovPortal</field>
</fields>

If you need to have full string of content in the field instead of individual word index, you need to have the following configurations for the field data to be tokenized.

<fieldMap type="Sitecore.ContentSearch.FieldMap, Sitecore.ContentSearch">
      <fieldNames hint="raw:AddFieldByFieldName">
              <field fieldName="customFieldName" storageType="YES" indexType="TOKENIZED" vectorType="NO" boost="1f" type="System.String" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider">
                <analyzer type="Sitecore.ContentSearch.LuceneProvider.Analyzers.LowerCaseKeywordAnalyzer, Sitecore.ContentSearch.LuceneProvider" />
              </field>
      </fieldNames>
</fieldMap>

The custom code should run on the item save event to save the index.  You can use the Index manager to rebuild the index to see the result as well.

Xing Liu

Xing has 5 years of experience with developing web solutions using ASP.NET and SQL Server. For the past 3 years, he has been actively engaged in website development projects using Sitecore CMS. He enjoys creating web solutions for clients using Sitecore CMS and, using its flexibility, provides cohesive and integrated solutions. Xing is a Sitecore Technology MVP, 2016.

Explore topics:

Other Blog Posts from Xing Liu