Creating Computed Index field in Sitecore 8

Why do we need Computed Indexed Field?

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.

Coding:-

  • Create a class which implements AbstractComputedIndexField
  • Use the method Sitecore.ContentSearch.ComputedFields namespace, then override the ComputeFieldValue method.
  • Create a config-include file which adds the computed index field to the index configuration.Here I will using the same example used in “Uli” Blogs, but I have made the changes code and config file so that it can work in Sitecore 8.
  • Author TemplateComputedField1
  • Publication Template

    ComputedField2

     

    Below code will create the IndexField name “AuthorPublications”. And this field will contain all the Publication ID related to the Author.

     

    public class AuthorPublications : AbstractComputedIndexField
    {
    public override object ComputeFieldValue(IIndexable indexable)
    {
    Item item = indexable as SitecoreIndexableItem;
    if (item == null)
    return null;
    if (item.TemplateName != “Author”)
    return null;
    return GetPublicationIDs(item);
    }

    private IEnumerable<Guid> GetPublicationIDs(Item authorItem)
    {
    return (from link in Globals.LinkDatabase.GetItemReferrers(authorItem, false)
    let sourceItem = link.GetSourceItem()
    where sourceItem != null
    where sourceItem.TemplateName == “Publication”
    select sourceItem.ID.Guid).ToArray();
    }

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

    Create a new Config file “ComputedIndexFields.config” and add the namespace & DLL name in the field

    <?xml version=”1.0″ encoding=”utf-8″ ?>

    <configuration xmlns:patch=”http://www.sitecore.net/xmlconfig/”&gt;
    <sitecore>
    <contentSearch>
    <indexConfigurations>
    <defaultLuceneIndexConfiguration type=”Sitecore.ContentSearch.LuceneProvider.LuceneIndexConfiguration, Sitecore.ContentSearch.LuceneProvider”>
    <fields hint=”raw:AddComputedIndexField”>
    <field fieldName=”AuthorPublications” storageType=”YES” indexType=”TOKENIZED”>Namespace.AuthorPublications, DLL Name </field>
    </fields>
    </defaultLuceneIndexConfiguration>
    </indexConfigurations>
    </contentSearch>
    </sitecore>
    </configuration>

    After adding the file in App_config/Include folder, Kindly check in Sitecore\admin\Showconfig.aspx

    Debugging using Visual Studio.

    1)      Open the Visual Studio -> Attached the debugger.

    2)      In Desktop-> Control Panel-> Indexing Manager-> Rebuild the Search Index.

    Also in Luke you can find the field

  •  

    ComputedField3

  •  

    References

    https://reasoncodeexample.com/2014/04/01/computed-index-fields-sitecore-7-content-search/

    http://www.3chillies.co.uk/blog/2015/10/code-how-to-create-a-computed-index-field-in-sitecore-8-solr-lucene

     

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s