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>

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" />

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 Liu is a Sitecore MVP with expertise in all aspects of ASP.NET and SQL Server development across a variety of industries including Government, Banking, Healthcare and Energy. He provides technical thought leadership and consulting to SymSoft and client development teams. Xing has deep expertise in using Sitecore Helix in an Agile delivery model to streamline the construction of complex Sitecore websites, portals and their associated authoring environments. Xing is also a globally recognized photographer with an Instagram following approaching 100,000.

Explore topics:

Other Blog Posts from Xing Liu