Make a Sitecore field multi-value

A Sitecore field can be indexed as a multi-value facet field if it contains a list of semicolon-separated values (semicolon is the default separator). For example, a Sitecore Single-Line Text field containing values abc;def;ghi can be set as a multi-value facet field.

Important

You can only configure multi-value facet fields with strings. This method won’t work to create multi-value fields with integers, floating points, or date/time values.

Configuring a field as a multi-value facet

  1. Open the Coveo.SearchProvider.Custom.config file with a text editor.

  2. In the fieldMap/fieldNames element, add the <fieldType> element below. You must replace mysitecorefield with the Sitecore field name (not the Translated Name).

     <fieldType fieldName="mysitecorefield" isMultiValue="true" settingType="Coveo.Framework.Configuration.FieldConfiguration, Coveo.Framework" />
    Note

    The fieldMap/fieldNames element is located in the defaultIndexConfiguration element. Field settings defined in the defaultIndexConfiguration element are applied to all indexes that reference the defaultIndexConfiguration element. To apply field configurations on a specific index, see Manage fields for a specific index.

  3. Save your changes.

  4. To synchronize these field settings changes into your search indexes, an indexing action must be performed. Alternatively, the /coveo/api/index/v1/indexes/synchronize API POST call can be used.

You can combine isMultiValue with the other settings presented in this section.

Changing the separator of multi-value fields

You can change the default separator (;) used for multi-value fields by adding the following element in the /configuration/sitecore/coveo/defaultIndexConfiguration element:

<MultipleValueSeparator>YOUR_SEPARATOR_HERE</MultipleValueSeparator>

Resolving GUIDs in a multi-value facet component

An out of the box C# class allowing you to retrieve specific fields from an item referenced by a field is already available and supports multi-value fields (see Coveo.SearchProvider.ComputedFields.ReferencedFieldComputedField).

However, this class only retrieves a field value from the referenced item. In other words, it won’t allow you to get the item name, display name or template ID, for example.

The following method allows you to retrieve the name of the referenced item.

  1. Implement a computed field that takes care of resolving these GUIDs to specific field values on the referenced item. Here is an example for a field of type Multilist named MultiSelect.

    namespace Coveo.Demos.Fields
    {
        public class MultivalueReferenceItemComputedField : IComputedIndexField
        {
            /// <inheritdoc />
            public string FieldName { get; set; }
            /// <inheritdoc />
            public string ReturnType { get; set; }
            /// <inheritdoc />
            public object ComputeFieldValue(IIndexable p_Indexable)
            {
                IIndexableDataField multiValueField = p_Indexable.GetFieldByName("MultiSelect");
                if (multiValueField != null) {
                    // The raw value of a Multilist field looks like this:
                    // {F90052A5-B4E6-4E6D-9812-1E1B88A6FCEA}|{F0D16EEE-3A05-4E43-A082-795A32B873C0}
                    // So we split it at the "|" character to retrieve the GUIDs.
                    string[] referencedItemGuids = multiValueField.Value.ToString().Split("|".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    List<string> referencedItemNames = new List<string>();
                    foreach (string referencedItemGuid in referencedItemGuids) {
                        // Then we retrieve the name of each item referenced by these GUIDs.
                        Item item = Context.ContentDatabase.GetItem(new ID(referencedItemGuid));
                        referencedItemNames.Add(item.Name);
                    }
                    // Finally, we join all those item names using the default separator (;) to get a properly-formatted field.
                    return String.Join(";", referencedItemNames.ToArray());
                }
                return null;
            }
        }
    }
  2. Open the Coveo.SearchProvider.Custom.config file with a text editor.

  3. Add the field as a computed field in the configuration under /coveo/defaultIndexConfiguration/fields[@hint='raw:AddComputedIndexField']:

    <field fieldName="multiselectfield">Coveo.Demos.Fields.MultivalueReferenceItemComputedField, Coveo.Demos.Fields</field>
    Note

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

  4. Set this computed field as multi-value in the Coveo Search Provider configuration file.