Sometimes you want to have a facet calculated on just a subset of the result or have multiple facets each being calculated on a different subset of the result. To enable this I’ve created a small extension project to the Episerver Find API, FacetFilter2Find, that enables passing a filter to TermsFacetFor(…) that will filter the result set when calculating the facet.

How to use the FacetFilter2Find extension

To use it you simply pass a filter when requesting the facet:

result = client.Search<Document>()
                        .TermsFacetFor(x => x.Category, x => x.Type.Match("pdf"))
                        .GetResult();

and fetch the resulting facet:

facet = result.TermsFacetFor(x => x.Category);

In order to specify multiple facets on a single field, each having a different filter, one must specify a custom name:

result = client.Search<Document>()
                        .TermsFacetFor(x => x.Category, x => x.Type.Match("pdf"), x => x.Name = "PdfCategories")
                        .TermsFacetFor(x => x.Category, x => x.Type.Match("doc"), x => x.Name = "DocCategories")
                        .GetResult();

and fetch the resulting facets:

pdfTypeFacet = result.Facets["PdfCategories"] as TermsFacet;
docTypeFacet = result.Facets["DocCategories"] as TermsFacet;

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