Adding search facets on computed fields in Sitecore 9 with SOLR

Adding facets to help content authors in searching content is always a great idea, especially when you have rather large buckets with loads of items.

One thing I noticed while trying to set this up, was that I needed untokenized fields in the index to make this work as expected, while text fields by default are tokenized in solr.

To understand this better, lets take an example of a bucket of products – say jewelry. I have a field called Jewelry Type on my products. If I wanted to give the content author an option to find jewelry by this field, I could add a facet on the existing field.

I would expect to see facet values like these:


But instead, I would see separate facets for each word, like ‘Engagement’, ‘Rings’, ‘Right’, ‘Hand’ etc. That’s certainly not what we need.

So to do that – instead of changing your solr settings to treat all fields of the certain type as untokenized (which would open a different can of worms!), we created a computed field to populate the Jewelry Type values into an untokenized field. This would treat the whole field value as a complete string and not break it down.

This is a relatively straightforward computed field:

using Foundation.Search.Solr.Helpers;
using Sitecore.ContentSearch;
using Sitecore.ContentSearch.ComputedFields;

namespace Foundation.Search.Solr.ComputedFields
	public class JewelryType : IComputedIndexField
		public object ComputeFieldValue(IIndexable indexable)
			SitecoreIndexableItem sitecoreIndexable = indexable as SitecoreIndexableItem;

			if (sitecoreIndexable == null) return null;

			if (sitecoreIndexable.Item.TemplateID.ToString() == Constants.ProductTemplateId)
				return sitecoreIndexable.Item["JewelryType"];

			return null;

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

Now we set up the facet with this new computed field created: /sitecore/system/Settings/Buckets/Facets

Using the template: /sitecore/templates/System/Item Buckets/Facet

Where the field name – matches the name of the computed field you created.


You could choose to mark this facet as global or local.
If your facet is very specific to a particular bucketed item template, you could as well mark it as local, and then add it on the particular bucket item only.



One thought on “Adding search facets on computed fields in Sitecore 9 with SOLR

Leave a Reply

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

You are commenting using your 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