Last active
March 14, 2016 23:46
-
-
Save RyanABailey/382ddb4693ef43b9ce11 to your computer and use it in GitHub Desktop.
Sitecore Lucene 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
var searchIndex = ContentSearchManager.GetIndex("MySearchIndex"); // Get the search index | |
var searchPredicate = BuildSearchPredicate(searchRequest); // Build the search predicate | |
using (var searchContext = searchIndex.CreateSearchContext()) // Get a context of the search index | |
{ | |
var searchResults = searchContext.GetQueryable<SearchModel>().Where(searchPredicate); // Search the index for items which match the predicate | |
var searchFacets = searchContext.GetQueryable<SearchModel>().Where(searchPredicate).FacetOn(x => x.Category).GetFacets(); | |
// Category facets | |
var categoryFacets = searchFacets.Categories.Where(x => x.Name == "categoryfacet").FirstOrDefault(); // Get the custom categoryfacet facet list | |
if (categoryFacets != null) | |
{ | |
foreach (var facet in categoryFacets.Values.Where(x => x.Name != null)) | |
{ | |
results.CategoryFacets.Add(new SearchFacet | |
{ | |
Count = facet.AggregateCount, | |
Value = facet.Name | |
}); | |
} | |
} | |
var pagedResults = searchResults.Page((searchRequest.Page - 1), searchRequest.PerPage).GetResults(); | |
} | |
public class SearchFacet | |
{ | |
public string Value { get; set; } | |
public int Count { get; set; } | |
} | |
public class SearchModel | |
{ | |
[IndexField("__smallupdateddate")] | |
public DateTime LastUpdated { get; set; } | |
[IndexField("_name")] | |
public string ItemName { get; set; } | |
[IndexField("_displayname")] | |
public string DisplayName { get; set; } | |
[IndexField("_templatename")] | |
public string TemplateName { get; set; } | |
[IndexField("introduction")] | |
public string Introduction { get; set; } | |
[IndexField("text")] | |
public string Maintext { get; set; } | |
[IndexField("exclude_from_search")] | |
public bool ExcludeFromSearch { get; set; } | |
[IndexField("page_description")] | |
public string PageDescription { get; set; } | |
[IndexField("categoryfacet")] | |
public string Category { get; set; } | |
[IndexField("customtitle")] | |
public string Title { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment