How to Index guids in Coveo keeping the original format – Part 2

Now that we know what conversion types are(see part 1) it’s time to implement a custom conversion type. Here are a few steps you can take to achieve that.

1- The first thing you need to do is create a new type and add a property ID of type Sitecore.Data.ID to it’s definition as implemented bellow:

public class CustomGuid
      public ID ID { get; set; }
      public CustomGuid(ID id)
           ID = id;

2- On the Coveo.SearchProvider.Custom.config file add a new computed field definition to the AddComputedIndexField section:

<field fieldName=”guidid” >MyNamespace.GuidID, MyProjectAssembly</field>

3- Now you need to create the corresponding type defined on step 2, which should implement IComputedIndexField:

public class GuidID : IComputedIndexField
      public string FieldName { get; set; }
      public string ReturnType { get; set; }
      public object ComputeFieldValue(IIndexable indexable)
            Precondition.NotNull(indexable, () => () => indexable);
            IItem item = new ItemWrapper(new IndexableWrapper(indexable));
            return new CustomGuid(item.ID);

Note that the return of the ComputeFieldValue method is a new instance of CustomGuid(defined on step 1) passing the item ID to the constructor. This is really important since the mapping is done on the type CustomGuid.

4- We need to implement now a custom type converter. This class should inherit from System.ComponentModel.TypeConverter

public class CustomGuidConverter : TypeConverter
      public CustomGuidConverter()
      public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
            if (destinationType == typeof(string))
                  return true;
            return base.CanConvertTo(context, destinationType);
      public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
            return ((CustomGuid)value).ID.ToString();

Note the method ConvertTo and its parameters. The parameter value type is CustomGuid defined on step 1 so we know we can cast to it. Also we know we stored the Sitecore item’s ID on the property ID so we can essentialy return it as as string.

5- On the Coveo.searchprovider.custom.config we need to map the custom converter with the type we defined on step 1. Look for the section indexFieldStorageValueFormatter and add the following patch definition:

<indexFieldStorageValueFormatter type="Coveo.SearchProvider.CoveoIndexFieldStorageValueFormatter, Coveo.SearchProvider" >
     <conversionTypes hint="raw:AddConverter">
          <!--Converts Short guid format to guid regular format-->
          <conversionType inputType="MyNamespace.CustomGuid" type="MyNamespace.CustomGuidConverter, MyAssembly"
patch:before = "*[1]" />

With this configuration Coveo will read at indexing time what the mapping should be.

  1. On Sitecore’s control panel rebuild your Coveo indexes.
  2. That’s it. After indexing if you open a document indexed you should look for the guidid field and you will see the guid indexed with its original value.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s