Skip to content

Instantly share code, notes, and snippets.

@MattisOlsson
Created September 30, 2016 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattisOlsson/71776288c75da9b7caa1e84b9cb27b41 to your computer and use it in GitHub Desktop.
Save MattisOlsson/71776288c75da9b7caa1e84b9cb27b41 to your computer and use it in GitHub Desktop.
Categories facet
public static class FacetExtensions
{
public static ITypeSearch<T> ContentCategoriesFacet<T>(this ITypeSearch<T> request) where T : ICategorizable
{
return request.HistogramFacetFor(x => x.Category, 1);
}
public static ITypeSearch<T> CategoriesFacet<T>(this ITypeSearch<T> request, Expression<Func<T, object>> fieldExpression) where T : ICategorizable
{
return request.HistogramFacetFor(fieldExpression, 1);
}
public static IEnumerable<CategoryCount> ContentCategoriesFacet<T>(this IHasFacetResults<T> result) where T : ICategorizable
{
return CategoriesFacet(result, x => x.Category);
}
public static IEnumerable<CategoryCount> CategoriesFacet<T>(this IHasFacetResults<T> result, Expression<Func<T, object>> fieldExpression) where T : ICategorizable
{
HistogramFacet facet = result.HistogramFacetFor(fieldExpression);
if (facet == null)
{
yield break;
}
var categoryRepo = ServiceLocator.Current.GetInstance<CategoryRepository>();
foreach (var value in facet.Entries)
{
Category category = categoryRepo.Get(value.Key);
if (category.IsNotNull())
{
yield return new CategoryCount(category, value.Count);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment