Implement and configure a computed field in your index

Computed fields let you add entirely new fields in your Coveo organization, in addition to the Sitecore template fields.

You can implement a computed field using the following steps:

Step 1: Implement the computed field

  1. Create a new C# class library project called Coveo.Custom. You’ll need to reference the Sitecore.ContentSearch assembly.

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

     using System;
     using Sitecore;
     using Sitecore.ContentSearch;
     using Sitecore.ContentSearch.ComputedFields;
     
     namespace Coveo.Custom
     {
         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 specific needs.

    Important
    • A computed field is processed for every Sitecore item that’s re-indexed. To minimize the impact on the duration of the indexing process

      • Filter out items that your computed field shouldn’t be added to.

      • Avoid doing HTTP requests.

      • Avoid querying a search index.

      • Ensure you optimize your code.

    • Since computed field logic runs in the context of Sitecore, you can take advantage of using the Sitecore API.

    Note

    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

    (Ensure the ComputeFieldValue method returns the datetime value in yyyy/MM/dd@HH:mm:ssZ format.)

    Date/time

    string

    String

    Note

    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 Coveo.Custom.dll to the bin folder of your Sitecore instance (typically <SITECORE_INSTANCE_ROOT>\bin or <SITECORE_INSTANCE_ROOT>\website\bin).

Step 2: Add the computed field in the configuration

  1. Open the Coveo.SearchProvider.Custom.config file in a text editor. This file is typically located in <SITECORE_INSTANCE_ROOT>\App_Config\Include\Coveo or <SITECORE_INSTANCE_ROOT>\website\App_Config\Include\Coveo.

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

    <coveo>
      <defaultIndexConfiguration>
        <fieldMap>
          <fieldNames hint="raw:AddFieldByFieldName">
        </fieldMap>
            <documentOptions>
              <!--
                ...
              -->
              <fields hint="raw:AddComputedIndexField">
  3. Add a new <field> element as an immediate child of the <fields hint="raw:AddComputedIndexField"> element.

     <field fieldName="mycoveofield">MyNamespace.ComputedFields.MyComputedField, MyNamespace.ComputedFields</field>

    Replace the fieldName and XML element text values with the name you want for the Coveo index field, the proper full class name, and the assembly name.

    Note

    This configuration creates the computed field on indexing.

    Important

    Don’t use special characters in computed field names.

  4. (Optional) Locate the <fieldNames hint="raw:AddFieldByFieldName"> element.

    <coveo>
      <defaultIndexConfiguration>
        <fieldMap>
          <fieldNames hint="raw:AddFieldByFieldName">
  5. (Optional) Add a new <fieldType> element as an immediate child of the <fieldNames hint="raw:AddFieldByFieldName"> element.

    <fieldType fieldName="mycoveofield" isSortable="true" isFacet="true" settingType="Coveo.Framework.Configuration.FieldConfiguration, Coveo.Framework" returnType="System.String"/>

    Replace the fieldName attribute value with the name you chose earlier for your Coveo index field. Replace the returnType attribute value with the appropriate value in the context.

    Note

    This configuration lets you specify special settings for the new field (for example, so that you may use the field as a facet or to sort search results).

Step 3: Rebuild your search indexes

Step 4: Validate the computed field has been added

Validate that your new field has been added on your items using the Content Browser (platform-ca | platform-eu | platform-au).

Warning

When running computed field code, check your logs for computed field errors indicating items aren’t being indexed. Implement proper exception handling to avoid these issues. For example, you might want to stop the process or set a fallback value for a computed field when suitable.

Step 5: Use your new field

Use the new field in whatever way is needed. For example, you may want to display it in a result template.