Code faceting within the Sitecore content search Skip to main content Skip to footer

Code faceting within the Sitecore content search

I was really impressed with what we were able to achieve in a recent project by simply leveraging the facets functionality of the Sitecore Lucene and SOLR integration. We used this functionality to produce a list of search filters, just like the ones seen on most e-commerce websites.     While performing a search within the content search you are able to call the .FacetOn() method, the first property for this method is the index field you wish to facet on, you do this via a simple LINQ link expression: (x => x.PropertyName).   The second parameter to FacetOn() is optional, however it is an integer and it’s a min count. What does that mean? It means that if you provide a min count, your facet results will only bring back facets that have been found >= to the min count set by you, I believe the default is 0.    The final call to the method will look something like: .FacetOn(x => x.PropertyName, 1);  You are able to chain these method calls to facet on different fields within the same query.  Let’s have a look at some code: 

Image of example code

The above code will get you started. We could refactor parts of this code for example, the extraction of the facet properties and make it a little more generic/reusable.  You aren’t limited to set properties you can facet on, you are able to create your own SearchResultItem and add your own properties to query and facet on (a post on custom result items will follow shortly). Furthermore, you are not limited to a single facet category, you are able to facet on as many properties as you want, however this will leave you with more FacetCategories that will need processing.

About the author

3chillies

Unlimited possibilities

3chillies

Get in touch today