Gravatar for ren.john@beaconfire-red.com

Question by renjohn, Jul 18, 2016 5:10 PM

Set Sitecore Field to Number

Is there a way to set a Sitecore field to be a floating decimal number? We have tried setting the returnType to Number, but that has no effect. When we reindex an item the fieldset still shows the field as a string. We need this field to be sortable by this number.

Gravatar for lbergeron@coveo.com

Comment by Luc Bergeron, Jul 19, 2016 8:18 AM

Is your Sitecore field a template field or a computed field? If it is a computed field, can you please include an example of the source code producing the issue and the XML configuration to include the field?

In my environment, setting the type of a template field to Number makes it appear as a Floating point in the fieldset.

Thanks

Gravatar for ren.john@beaconfire-red.com

Comment by renjohn, Jul 19, 2016 10:11 AM

This is a computed field.

The fieldname setting:

<fieldType fieldName="WhatsTrendingScore" isSortable="true" returnType="Number" isFacet="false" isMultiValue="false" includeForFreeTextSearch="false" settingType="Coveo.Framework.Configuration.FieldConfiguration, Coveo.Framework" />

The field setting:

<field fieldName="WhatsTrendingScore">ACC.Coveo.Custom.ComputedFields.WhatsTrendingScoreComputedField, ACC.Coveo.Custom</field>
Gravatar for ren.john@beaconfire-red.com

Comment by renjohn, Jul 21, 2016 9:05 AM

That worked, thanks! This should be added to the documentation.

1 Reply
Gravatar for lbergeron@coveo.com

Answer by Luc Bergeron, Jul 20, 2016 10:17 AM

I was able to reproduce the issue you are describing. It appears that for some reason the returnType property was not getting set to Number by the configuration file.

Setting the property explicitly from the computed field did the trick. Here is a very simplified computed field that works in my environment.

public class MyNumberComputedField : IComputedIndexField
{
    public object ComputeFieldValue(Sitecore.ContentSearch.IIndexable p_Indexable)
    {
        float? result = null;

        // Your computation logic goes there...

        return result.HasValue ? result.Value.ToString(NumberFormatInfo.InvariantInfo) : null;
    }

    public string FieldName
    {
        get;
        set;
    }

    public string ReturnType
    {
        get { return "Number"; }
        set
        {
            // do nothing
        }
    }
}

Cheers

Ask a question