EPiServer Find Sample

  • Basic
  • Sorting
  • Paging
  • Highlighting
  • Filters
  • Facets
  • Autocomplete
  • Map

Facets Drill down into search results

Try searching for "hotel" to get many hits and use the links to the right to filter the results.

View Source
var query = client.Search<Hotel>()
    .For(q)
    .TermsFacetFor(x => x.Chain)
    .TermsFacetFor(x => x.Location.Country.Title)
    .HistogramFacetFor(x => x.StarRating, 1);

//Apply filters added by facet links
if (!string.IsNullOrWhiteSpace(chain))
{
    query = query.Filter(x => x.Chain.MatchCaseInsensitive(chain));
}

if (!string.IsNullOrWhiteSpace(country))
{
    query = query.Filter(x => x.Location.Country.Title.MatchCaseInsensitive(country));
}

if (rating.HasValue)
{
    query = query.Filter(x => x.StarRating.Match(rating.Value));
}

var results = query.Select(x => new SearchHit
    {
        Title = !string.IsNullOrEmpty(x.Name.AsHighlighted()) ? x.Name.AsHighlighted() : x.Name,
        Url = x.Website,
        Location = new List<string> { x.ShortAddress, x.Location.Title, x.Location.Country.Title }.Concatenate(", "),
        StarRating = x.StarRating
    })
    .GetResult();