Nested hierarchical faceting with Episerver Find

It's kind of unusual when the most relevant information you can find on something is coming up to 6 years old, but whenever I need hierarchical faceting in Episerver Find I always end up using HierarchicalFacet2Find and the corresponding blog post as a reference.

Recently I needed exactly this hierarchical facet functionality but the facet needed to be derived from a nested field, with a bit of tweaking this turned out to be very easy to achieve.

It was fairly trivial to reuse all the existing logic when resolving hierarchical facets, which meant there were only really a couple of things to do, namely: adding two HierarchicalFacetFor extension methods (one for requesting a facet when searching, the other for extracting from the search result). Both added methods take a lambda expression specifying which field contains the nested items.

I won't discuss the code too much (because I didn't write that much of it!), but it's all available on GitHub so you can take a look at the HierarchicalFacetExtensions class here and purloin what you need.

Usage

Usage remains exactly the same as before, but, if you want a nested hierarchical facet your class should have a nested field with the corresponding nested object having a Hierarchy property:

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

    public IEnumerable<Section> Sections { get; set; }
}

public class Section
{
    public string Name { get; set; }

    public Hierarchy Hierarchy { get; set; }
}

You can then assign the hierarchy path in exactly the same way as before...and specify the nested field when requesting the hierarchical facet during a search:

result = client.Search<Document>()
            .HierarchicalFacetFor(x => x.Sections, x => x.Hierarchy)
            .GetResult();

As you'd expect, getting it from a result can now also take the nested field:

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

Well, that was simple! It's always great when something turns out to be much easier than you expect, and this case it's a testament to how sharing code can really save time.

Comments

There are zero comments 😢