How to create a Computed index field

In Sitecore 7.1 the new indexes are very powerful and allow you to do stuff easily that in Sitecore 6 took quite an effort. An example of that is if you want to tag items and show lists of the items based on the tagging. This can be achieved with a computed index field and some good old Sitecore work. So lets look at the first part of the task and add a tag to the index at hand.

Tagging items

In order to tag items in Sitecore we first need to create a template for the tag itself. In this case I am creating a tag called “ProductType”. But remember, this is not only useful for products, anything could be tagged and listed. For example articles could be tagged with keywords or a region it applies to and so on.

So lets start by creating the data template for the product type. I called mine __ProductType. Remember that data templates always should be used in order to store the fields on a separate template from the template that is actually used in the content tree, this is to make the most of the inheritance of templates and to keep the solution clean.

And lets also add the template that inherits the data template. I called the template ProductType and in the content tab of the template I added __ProductType in the base templates.

The repository of tags

On to the content. Create a repository of tags somewhere in your content tree. I have created mine under /sitecore/content/Settings/Tagging/ProductTypes and I have added three product types.

Tagging template

Back to the templates. Create a data template that allows tagging of products. It should contain one field that allows tagging of products. I will only allow one tag per product so I chose a Droplink with the source set to /sitecore/content/Settings/Tagging/ProductTypes and called the template __ProductTagging.

And then just go ahead and add the data template to the items that you want to be taggable. This is the advantage of using pure data templates, now you are free to add the tagging to any other template. I added mine to the product template.

Create some products somewhere and add the tags to them.

The code for the computed index field

Now lets leave Sitecore for a moment and enter visual studio. Create the code that is needed for the index to be able to get the values of the chosen tags on your tagged products. Remember that it is just the ids that are actually stored as content in the Product type field of the product so a lookup of the tag field itself would be useless (unless you are remarkably good at remembering GUIDs).

Create a class called ProductTypeValue and let it implement IComputedIndexField.

Configuring the index to use the new field

Almost there, the only thing left to do is to add a line to the configuration file for the index. Open the Sitecore.ContentSearch.Lucene.DefaultIndexConfiguration.config in the includes folder. Find this element in the xml file “/configuration/sitecore/contentSearch/configuration/defaultIndexConfiguration/fields/field[@fieldName=’isbucket_text’]” and add the following line after it:

All done!

Go back to Sitecore and rebuild the index for the master database. Select the product container in the content tree and select the search tab. Type “producttype:Digital camera” (or whatever your product types are called) in the search box and you should get a result that looks something like this:

What is the use?

So after this work the items can be found from within Sitecore, that’s all fine and good but not great. The power of this comes to show when you couple this functionality with the page editor and the use of search based datasources to populate content. But more about that in a later post.