Thursday, March 23, 2017

Sitecore: Creating Computed Fields in Sitecore 8

Background

Sitecore has a built-in Lucene search engine that indexes all the standard template fields and values.  You can easily add new fields to templates that are created along the way.  Adding a field to the index for a standard number or string field is relatively straight-forward.  Just define an XML element in the

\App_Config\Include\Sitecore.ContentSearch.Lucene.DefaultIndexConfiguration.config

 under the

<fieldNames hint="raw:AddFieldByFieldName">

section and you are all set.  But what about fields that need some level of manipulation before being indexed?  For example, a content item could have a datetime field which consists of data for a full timestamp but all you need is the month to categorize a group of content items.  We can pull out all the items and then programmatically pull out the month per item but efficiency is lost since so much processing has to be done on runtime.  A better way to approach this is to do all the processing at index time and create computed fields to accomplish this.

Approach

Creating computed fields in Sitecore 8 has been made easier than previous versions.  You use to inherit from an interface and have to implement all the properties and methods.  Starting with version 8, there is now an abstract class to inherit from and minimal implementation is needed.  A barebones class for a computed field looks something like this:



using System;
using System.Collections.Generic;

using Sitecore.ContentSearch;
using Sitecore.ContentSearch.ComputedFields;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;

namespace MyProject.Utility.Search.ComputedFields
{
    public class TemplateIdAsString : AbstractComputedIndexField
    {
        public override object ComputeFieldValue(IIndexable indexable)
        {
            Item obj = (Item)(indexable as SitecoreIndexableItem);

            Sitecore.Data.ID templateId = obj.TemplateID;
            if (!templateId.IsNull)
                return (object)templateId.ToString().Replace("-",string.Empty)...

            return (object)"";
        }
    }
}



This computed field basically removes all the dashes from the templateID guid and converts to string format. All the data manipulation and processing is done inside the ComputeFieldValue method. When all work is done here, make sure you create a computed field definition in the config file above in the section

<fields hint="raw:AddComputedIndexField">

A simple example for the above field is:

<field fieldName="templateidasstring">MyProject.Utility.Search.ComputedFields.TemplateIdAsString,MyProject.Utility</field>



This basically sets all properties of the field to the default values. If you need to stray from the defaults, you can go back to the

<fieldNames hint="raw:AddFieldByFieldName">

section and define the field again there with custom field properties like this:

<field fieldName="templateidasstring" storageType="YES"  indexType="TOKENIZED"    vectorType="NO" type="System.String" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />
             


Run the index manager and verify everything in Luke that the computed field is now part of the index.







You have now created your first computed field in Sitecore. You can search on computed fields in a traditional Lucene search or via the ItemService API.








No comments:

Post a Comment