Gravatar for loebrandy@gmail.com

Question by rloeb, Nov 10, 2016 6:50 PM

Date as computed field not stored as date

We're trying to create a computed date field but it keeps getting stored/created in coveo as a string.

settings:

 <fieldType fieldName="computedTKMReviewerSignoffDateField" isExternal="true" isFacet="true" isMultiValue="false" isSortable="true" settingType="Coveo.Framework.Configuration.FieldConfiguration, Coveo.Framework" type="Sitecore.Data.ID, Sitecore.Kernel" returnType="System.Date" />


<field fieldName="computedTKMReviewerSignoffDateField" itemFieldName="TKM Reviewer Signoff Date">PwC.TTD.TKM.Web.Componets.Index.Coveo.ComputedFields.ComputedDateField, PwC.TTD.TKM.Web.Componets</field>

namespace PwC.TTD.TKM.Web.Componets.Index.Coveo.ComputedFields
{
public class ComputedDateField : IndexField
{
    public ComputedDateField(XmlNode configuration)
        : base(configuration)
    {

    }

    public override string ReturnType
    {
        get
        {
            return "datetime";
        }
        set
        {
            base.ReturnType = value;
        }
    }


    public override object ComputeFieldValue(IIndexable p_Indexable)
    {
        SitecoreIndexableItem indexableItem = p_Indexable as SitecoreIndexableItem;

        if (indexableItem != null)
        {
            IIndexableDataField dateField = indexableItem.GetFieldByName(ItemFieldName);

            if (dateField != null && dateField.Value != null)
            {
                var dateVal = dateField.Value.ToString();
                if (!string.IsNullOrEmpty(dateVal))
                {
                    DateTime date = DateUtil.ParseDateTime(dateVal, DateTime.MaxValue);
                    if (date != DateTime.MaxValue)
                    {
                        var dateStr = date.ToString(Settings.CoveoIndexDateFormat, CultureInfo.InvariantCulture);
                        return dateStr;
                    }
                }
            }
        }

        return null;
    }
}

Anything jumping out as wrong?

Gravatar for sbelzile@coveo.com

Comment by Sébastien Belzile, Nov 11, 2016 9:15 AM

There are 2 things I think about that could go wrong.

  1. Could you try setting the attribute "fieldTypeName" to "datetime" on your "fieldType" node?
  2. What happens if you remove the "fieldType" node?
1 Reply
Gravatar for lbergeron@coveo.com

Answer by Luc Bergeron, Nov 11, 2016 10:02 AM

Hi,

I was able to reproduce the issue with a computed field similar to yours. Setting returnType="datetime" on the computed field declaration did the trick.

In your specific case:

<field fieldName="computedTKMReviewerSignoffDateField" itemFieldName="TKM Reviewer Signoff Date">PwC.TTD.TKM.Web.Componets.Index.Coveo.ComputedFields.ComputedDateField, PwC.TTD.TKM.Web.Componets</field>

would become:

<field fieldName="computedTKMReviewerSignoffDateField" itemFieldName="TKM Reviewer Signoff Date" returnType="datetime">PwC.TTD.TKM.Web.Componets.Index.Coveo.ComputedFields.ComputedDateField, PwC.TTD.TKM.Web.Componets</field>

After the change, CES gave errors because the field type changed and the field was referenced by many field sets at the same time. Performing a second rebuild fixed it.

Cheers

Gravatar for loebrandy@gmail.com

Comment by rloeb, Nov 15, 2016 6:33 PM

Thanks will try.

Ask a question