Coveo for Sitecore 5 is now available!

Making 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 a multi-value facet field.

Note that 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

To configure 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 a new element like the following (you must replace mysitecorefield with the name of the Sitecore field):

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

    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 Managing Fields for a Specific Index.

  3. 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 (see Synchronize Coveo for Sitecore Configuration).

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 of the Coveo.SearchProvider.Custom.config file:

<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 instance.

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>
    

    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 (see Configuring a Field as a Multi-Value Facet).
  5. 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 (see Synchronize Coveo for Sitecore Configuration).
Recommended Articles