THIS IS ARCHIVED DOCUMENTATION

Implementing and Configuring a Computed Field in Your Index

Computed fields allow you to add entirely new fields in your search indexes, in addition to those of Sitecore.

You can implement a computed field using these four steps:

Step 1: Implement the Computed Field

  1. Create a new C# class library project.

  2. Create a new class that inherits from Sitecore.ContentSearch.ComputedFields.IComputedIndexField.

     using System;
     using Sitecore;
     using Sitecore.ContentSearch;
     using Sitecore.ContentSearch.ComputedFields;
    
     namespace MyNamespace.ComputedFields
     {
         public class MyComputedField : IComputedIndexField
         {
             /// <remarks>
             /// This property should be left empty. Instead, use the FieldName attribute in the configuration file to
             /// specify the name of the computed field.
             /// </remarks>
             public string FieldName { get; set; }
                
             /// <remarks>
             /// This property can optionally be used to specify the data type of the Sitecore field being currently processed. So instead of
             /// being automatically handled as a string (which is the default behavior), the field will be converted into the corresponding
             /// Coveo data type. If you leave the property as is, the value returned by ComputeFieldValue will be converted into 
             /// a string automatically. Note that you can't specify the return type of the field in the configuration file.
             /// </remarks>
             public string ReturnType { get; set; }
             
             /// <remarks>
             /// This method is what actually determines the value of the computed field.
             /// </remarks>
             public object ComputeFieldValue(IIndexable p_Indexable)
             {
                 // Returning null means that the field won't be created.
                 // By default, any returned value is automatically converted into a string.
                 return null;
             }
         }
     }
    
  3. Customize the class to meet your particular needs.

    The ReturnType property should be set to the Sitecore data type of the value returned by the computed field. Ultimately, this value is converted to the corresponding Coveo data type. If you don’t set any return type, the value is automatically converted to a string, and therefore be of type String in the Coveo index. Here are the supported Sitecore data types along with their corresponding Coveo data types.

    Sitecore data type Coveo index data type
    Integer Integer
    Number Floating point
    date Date/time
    datetime Date/time
    string String

    To access the built-in fields of the item being processed by the computed field, cast the IIndexable parameter of the ComputeFieldValue method to IIndexableBuiltinFields. For example, do the following to access the template name:

     public object ComputeFieldValue(IIndexable p_Indexable)
     {
         IIndexableBuiltinFields fields = (IIndexableBuiltinFields)p_Indexable;
         string templateName = fields.TemplateName;
         ...
     }
    
  4. Compile the project.

  5. Copy the resulting assembly to the bin folder of your Sitecore instance (typically <SITECORE_INSTANCE_ROOT>\website\bin).

    Remember that a computed field is processed for every Sitecore item that’s re-indexed, so it’s up to you to filter out items that your computed field shouldn’t be added to.

Step 2: Add the Computed Field in the Configuration

  1. Open the Coveo.SearchProvider.Custom.config file (typically located in <SITECORE_INSTANCE_ROOT>\website\App_Config\Include\Coveo).

  2. Locate the <fields hint="raw:AddComputedIndexField"> element.

    If you’re unable to find this element, you might need to copy it from the Coveo.SearchProvider.config file. However, note that you’re discouraged from modifying this file, as it may cause issues during upgrades.

  3. Add a new field element under it:

     <field fieldName="mycomputedfield">MyNamespace.ComputedFields.MyComputedField, MyNamespace.ComputedFields</field>
    
  4. Locate the <fieldNames hint="raw:AddFieldByFieldName"> element.

  5. In this node, add the following element:

     <fieldType fieldName="MyComputedField" isSortable="true" isFacet="true" type="System.DateTime" settingType="Coveo.Framework.Configuration.FieldConfiguration, Coveo.Framework" returnType="System.DateTime"/>
    

Step 3: Rebuild Your Search Indexes

  1. Rebuild your search indexes, or at least re-index the Sitecore items for which you need the new field. You can use either the Indexing Manager or the Re-Index Tree feature to do this.

  2. Validate that your new field has been added on your items using the Index Browser (CES Admin Tool > Content > Index Browser).

    Validate that your new field has been added on your items using the Content Browser (Coveo Administration Console > Search Content > Content Browser).

Step 4: Use Your New Field

Use the new field in whatever way is needed. For example, you may want to display it in a search interface (see Adding a Field to a Result Template).