A quite common use case for facets is to show a listing of the number of documents in a result for the different categories on the site (quite common is maybe an understatement as this is often the “hello world!” of faceting). A document can occur in maybe one or more categories and this is where your search index really stands out since it doesn’t care if you have one or two categories associated with the document it will return your facet in no time anyway. Sometimes your categories have a hierarchical structure that you want to reflect in your facet. Lets say that you have documents about cars and want to have a category tree based on manufacturer and name, ie. Volvo/XC60. How can you achieve a facet where you get an aggregated count for each level in the category tree? For example:

Volkswagen (7)
    Passat (5)
    Tiguan (2)
Volvo (10)
    V70 (5)
    V60 (2)

The simplest way of doing this is by associating each document with all levels for each of its categories. Then by using a terms facet when fetching your result you will get an aggregated count for each node in your category tree. Voila, there is your hierarchical facet but with just a few lines of code you can get Find to do all that dirty work of your hands and all you have to do is to pass a category string (each level separated by a ‘/’) and return a facet that parse the result and reflects the nested structure of the category tree. I will leave out the implementation details but at HierarchicalFacet2Find you can fetch your own copy of the code that does that work for you.

How to use the HierarchicalFacet2Find extension

Add a Hierarchy property to the document:

public class Document
    public string Id { get; set; }

    public Hierarchy Hierarchy { get; set; }

Set the hierarchy path:

document.Hierarchy = "A/B/C/D";

Index and request a HierarchicalFacet when searching:

result = client.Search<Document>()
            .HierarchicalFacetFor(x => x.Hierarchy)

Fetch it from the result:

facet = result.HierarchicalFacetFor(x => x.Hierarchy)

Loop over the nested hierarchy paths:

foreach(var hierarchyPath in facet)

    foreach (var subHierarchyPath in hierarchyPath)

I hope you may find this useful when making your site awesome with EPiServer Find!