Create a computed index field

Abstract

Shows how you create a computed index field.

In simple indexing scenarios, an indexer takes a text and places it in an index. Sometimes you need more control over how the data is stored or, more importantly, what data is stored. You use computed fields to look up data and to use logic to specify what Sitecore stores in an index. For example, Sitecore uses computed fields to store the parsed Created Date of an item. A computed index field is processed for every Sitecore item that gets indexed.

To create a computed index field:

  1. Create a class that implements the Sitecore.ContentSearch.ComputedFields.IComputedIndexField interface.

  2. Implement simple string properties named FieldName and ReturnType. The FieldName string is the name the field uses in the index.

  3. Implement a method called ComputeFieldValue(). This method accepts an argument that implements the Sitecore.ContentSearch.IIndexable interface and that specifies the data to index. It returns an object that represents the value for the field.

    Your class will look like this example:

    public class IsClone : IComputedIndexField
    {
        public object ComputeFieldValue(IIndexable indexable)
        {
            Item item = (Item) (indexable as SitecoreIndexableItem);
            return item.IsClone;
        }
    
        public string FieldName { get; set; }
    
        public string ReturnType { get; set; }
    }
  4. Add the computed field to the configuration.