Fredrik
Fredrik

Reputation: 13

Episerver find facets

I am building an restaurantfinder website with EPiServer and I'm using EPiFind to search on the site. I've encountered a problem though. I'm trying to facet a pagetype in the pagetree, although I'm only searching in one specific pagetype. My goal is to be able to print out the titles of the pagetype. So if I search for example Sweden, all cities that's in the pagetree should be listed with their names in the facet.

My code:

if (query == null && tags == null && cities == null)
{
    return View();
}

var q = SearchClient.Instance.Search<RestaurantPage>()
    .For(query)
    .TermsFacetFor(x => x.CityPage.HeadLine);

if (!string.IsNullOrWhiteSpace(cities))
{
    q = q.Filter(x => x.HeadLine.MatchCaseInsensitive(cities));
}
var results = q.Select(x => new SearchHit
{
    Title = x.HeadLine,
    Url = x.LinkURL,
    Tag = x.Tags,
    Adress = x.Adress,
    Latitude = x.Latitude,
    Longitude = x.Longitude,
    Image = x.RestaurantImage
}).GetResult();

ViewBag.Query = query;
ViewBag.Id = results.ProcessingInfo.ServerDuration;
ViewBag.Hits = results.TotalMatching;

var facets = new List<FacetResult>();      
var testFacet = (TermsFacet)results.Facets["CityPage.HeadLine"];
var testLinks = new FacetResult("Cities", testFacet.Terms.Select(x => new FacetLink
{
    Text = x.Term,
    Count = x.Count,       
}));
facets.Add(testLinks);         

ViewBag.Filters = new List<FacetLink>();
if (!string.IsNullOrEmpty(tags))
{
    ViewBag.Filters.Add(new FacetLink
    {
        Text = tags,
        Url = Url.QueryBuilder(currentPage.ContentLink).AddSegment("?query=" + query).ToString()
    });
}


return View(new SearchResult(results, query) {Facets = facets});  

Upvotes: 0

Views: 1378

Answers (1)

jskzyb
jskzyb

Reputation: 161

In order to retrieve the facets, you could do:

results.TermsFacetFor<RestaurantPage>(x => x.CityPage.HeadLine)

Upvotes: 1

Related Questions