Created
September 30, 2016 08:25
-
-
Save MattisOlsson/71776288c75da9b7caa1e84b9cb27b41 to your computer and use it in GitHub Desktop.
Categories facet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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