Create a computed index field

Current version: 10.0

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. The returnType string refers to the Solr data type that the Sitecore field type is mapped to within typeMatches. You can find all available return types listed under <typeMatches hint="raw:AddTypeMatch>.

  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:

    RequestResponse
    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; }
    }
    Note

    Avoid use of local variables. Sitecore reuses the same instance of the computed index field class. The ComputeFieldValue(IIndexable) method can be invoked simultaneously for the same computed index field with different IIndexable arguments. This might lead to inconsistent data in the search index or errors during indexing. If you cannot avoid the use of local variables, use [ThreadStatic] or thread synchronization mechanisms.

  4. In the Sitecore.ContentSearch.Solr.DefaultIndexConfiguration.config configuration file, locate <fields hint="raw:AddComputedIndexField">. You can read more about adding the computed field to the configuration.

  5. Add a <field> element to AddComputedIndexField and reference your custom class and assembly within it. The computed field is mapped to a Solr dynamic field, for example:

    RequestResponse
    <fields hint="raw:AddComputedIndexField">
        <field fieldName="nameOfComputedIndexField" returnType="string">
         YourNamespaceHere.ComputedFields.IsClone, YourNamespaceHere
        </field>
    </fields>
    Tip

    The example refers to the IsClone class defined in step 3 of this topic. The Sitecore computed field nameOfComputedIndexField is mapped to the Solr string type field.

  6. Rebuild your search indexes using the Indexing Manager (accessed by opening the Indexing app from the control panel).

Do you have some feedback for us?

If you have suggestions for improving this article,