Gravatar for slangevin@coveo.com

Question by Simon, Mar 2, 2016 3:45 PM

Computed field not added to my items

I did a computed field in Coveo for Sitecore and doesnt seems to be working. I am using template name and content path for it and the new value doesnt seems to be indexed.

In the config file:

<fieldType fieldName="contenttypecustom" isFacet="true" settingType="Coveo.Framework.Configuration.FieldConfiguration, Coveo.Framework"/>
<field fieldName="contenttypecustom">MyAssembly.ContentTypeComputedField, MyAssembly</field>

And the code:

class ContentTypeComputedField : IComputedIndexField {
    public string FieldName { get; set; }
    public string ReturnType { get; set; }

    public object ComputeFieldValue(IIndexable p_Indexable)
    {
        IIndexableDataField contentField = p_Indexable.GetFieldByName("_templatename");

        if (contentField != null)
        {
            string contentType = contentField.Value.ToString();

            if (contentType == ".....")
            {
                return "Product";
            }
            else if (contentType == "...")
            {
                return "Rewards";
            }
            ......

            else
            {
                return "Other";
            }
        }

        return null;
    }
}

Do you know what might be wrong?

1 Reply
Gravatar for slangevin@coveo.com

Answer by Simon, Mar 2, 2016 4:03 PM

In the config, the case of the elements and attributes is very important. Your field names should be the same in the 2 sections. You didn't hardcode the returnType of your computed field in the code so you need to specify it in the config.

Here is your config:

<fieldMap type="Coveo.Framework.Fields.CoveoFieldMap, Coveo.Framework">
    <fieldNames hint="raw:AddFieldByFieldName">
        <fieldtype fieldname="contenttypecustom" isfacet="true" settingtype="Coveo.Framework.Configuration.FieldConfiguration, Coveo.Framework"/>
    </fieldNames>
</fieldMap>
<fields hint="raw:AddComputedIndexField">
    <field fieldname="contenttypecustom">MyAssembly.ContentTypeComputedField, MyAssembly</field> 
</fields>

Here is what it should be:

<fieldMap type="Coveo.Framework.Fields.CoveoFieldMap, Coveo.Framework">
    <fieldNames hint="raw:AddFieldByFieldName">
        <fieldType fieldName="contenttypecustom" isFacet="true" settingType="Coveo.Framework.Configuration.FieldConfiguration, Coveo.Framework"/>
    </fieldNames>
</fieldMap>
<fields hint="raw:AddComputedIndexField">
    <field fieldName="contenttypecustom" returnType="System.String">MyAssembly.ContentTypeComputedField, MyAssembly</field>
</fields>

Note that the 2 fieldName attributes are set to the same value. This is indeed needed for your computed field to be indexed correctly as a facet.

After this, if your code doesn't work as expected, attach Visual Studio to your w3wp.exe process, set a breakpoint, start the indexing of an item and step in your code to see what's the problem.

Ask a question