Twitter LinkedIn

CODE HOW TO CREATE A COMPUTED INDEX FIELD IN SITECORE 8 SOLR LUCENE

  • By Andrew Ward
Andrew Ward

Computed fields are a great way of storing calculated information while Sitecore indexes your items as opposed to using more resource to calculate it on the fly for use in your sublayouts/layouts.

An example for when you might want to use a computed index field is if you imagine you have a couple of blog categories and you want to provide a count of how many blog articles that refer to that category. Granted you will be able to use the Sitecore links database (which we are going to use but not at runtime) or even facets which are provided by Lucene/Solr, however this will use more resource, and why use that when you can calculate it once and use it freely without performance penalties?

Let’s get started on our blog count computed field.

In our project, we need to add a class inheriting from the base AbstractComputedIndexField class located within the Sitecore.ContentSearch.ComputedFields namespace, then override the ComputeFieldValue method. This method is going to contain all the code that is going to check if the item being index is a blog category, if it is we are then going to check the Sitecore links database to see how many blog articles refer to it .

namespace Sitecore.Playground.ComputedFields
{
    using ContentSearch;
    using ContentSearch.ComputedFields;
    using Data.Items;
    using System.Linq;

    public class BlogCategoryReferenceCount : AbstractComputedIndexField
    {
        public override object ComputeFieldValue(IIndexable indexable)
        {
            // item is currently being indexed.
            Item item = indexable as SitecoreIndexableItem;

            // null check on item
            if (item == null)
            {
                return null;
            }

            // set count variable
            int? count;

            if (item.TemplateName.Equals("Blog Category"))
            {
                //get referrers of item: blog category, false: ignore standard values
                //count: all referrers where template name equals blog article
                count = Globals.LinkDatabase
                            .GetItemReferrers(item, false)
                            .Count(c => c.GetSourceItem().TemplateName.Equals("Blog Article"));
            }
            else
            {
                count = null;
            }

            // if count greater than zero return count else return null
            return count;
        }
    }
}

Now that we have our method it’s time to let Sitecore know about our new field, we do that by adding it to the computed field section in our default index configuration config:

<fields hint="raw:AddComputedIndexField">
      <field fieldName="blogcategoryreferencecount">
           Sitecore.Playground.ComputedFields.BlogCategoryReferenceCount , Sitecore.Playground
      </field>
</fields>
3chillies Reading (HQ) 4th Floor, The Blade, Abbey Square Reading Berkshire RG1 3BE 0118 931 4196 Find us
This website is hosted Green - checked by thegreenwebfoundation.org

Our Partners

  • microsoft partner logo
  • sitecore logo
  • umbraco logo
  • Optmizely logo
  • uMarketingSuite logo
  • Cookiebot
scroll back to the top of the current web page