Faceting on product specifications with SOLR computed fields in Sitecore

In most product repositories, we come across product specification sets for each product, which usually comprise of a key value pair.
We had one such scenario in our project, and we came across the requirement to facet on various specification keys.

We have currently added a list of comma separated (key, value) pairs in the specifications computed field. The computed field needed to be declared as a string collection:

<field fieldName="productspecificationstext" returnType="stringCollection" storageType="YES" indexType="string">

And in the code for the computed field (implementing IComputedIndexField), you will want to split this list and return it as a List.
So this

public object ComputeFieldValue(IIndexable indexable)

would return List<string>

Here’s a sample from the index:


So then we facet on this field, and filter out the facets that start with (specification key + ‘,’) for the specification key we want the facets for and remove the preceding key and ‘,’.
So if I were to facet on size specification for the below data (provided we had a separator between the specification key and value), we would get:


Large (1)
Small (3)
Medium (1)

This way, we use the same computed field to facet on any kind of specification.

The method used would be the Sitecore.ContentSearch method: