Skip to content

Instantly share code, notes, and snippets.

@mattbrailsford
Created June 3, 2020 18:07
Show Gist options
  • Save mattbrailsford/73a0c30b73287d2b6166def056208bac to your computer and use it in GitHub Desktop.
Save mattbrailsford/73a0c30b73287d2b6166def056208bac to your computer and use it in GitHub Desktop.
Vendr Faceted Search
// Perform a faceted search based on product categories
var dir = new DirectoryInfo(((LuceneIndex)index).LuceneIndexFolder.FullName);
using (var searcher = new IndexSearcher(FSDirectory.Open(dir), false))
using (var factedSearcher = new SimpleFacetedSearch(searcher.IndexReader, new string[] { "searchCategory" }))
{
var queryParser = new QueryParser(Version.LUCENE_30, "", new KeywordAnalyzer());
var query = queryParser.Parse(sb.ToString());
var queryResults = factedSearcher.Search(query, ps * p);
var facetedResults = new Dictionary<string, PagedResult<IPublishedContent>>();
foreach (SimpleFacetedSearch.HitsPerFacet hpg in queryResults.HitsPerFacet)
{
facetedResults.Add(hpg.Name.ToString(), new PagedResult<IPublishedContent>(hpg.HitCount, p, ps)
{
Items = hpg.Documents.Skip(ps * (p - 1)).Select(x => UmbracoContext.Content.GetById(int.Parse(x.Get("id")))).ToList()
});
}
result.Facets = facetedResults;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment