Gabbar
Gabbar

Reputation: 4066

Lucene fields default value in Sitecore 7

Is there a way to force Lucene (in Sitecore 7) to have a default value when there is no value associated for a field? I've been trying to do an empty string or null value comparison on a field but it isn't working. I want all items where this particular field does not have a value to be excluded from my result set.

Thanks

Upvotes: 1

Views: 307

Answers (1)

Martin Davies
Martin Davies

Reputation: 4456

You can create a computed field based on the original field. If it's empty, then you return a default value:

public class NullOrEmptyComputedField : IComputedIndexField
{
    public object ComputeFieldValue(IIndexable indexable)
    {
        Item item = indexable as SitecoreIndexableItem;
        if (item == null)
            return null;

        // We return a default value if the target field is empty
        if (String.IsNullOrEmpty(item["originalField"]))
            return "_EMPTY_";

        return item["originalField"];
    }

    public string FieldName { get; set; 
    public string ReturnType  { get; set; }
}

See the following articles for tips on creating computed fields:

Upvotes: 1

Related Questions