Code how to create a computed index field in Sitecore 8 Solr Lucene
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 .
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:
Related Blog posts
Unlimited possibilities
3chillies
Get in touch today