Commerce product seach: using nodes/categories in facets

Vote:
 

Hi,

I'm doing a product search for Episerver Commerce site using Find.

Most of the examples and the documentation use the products properties for faceting and filtering the search, but in our case we would like to filter by the all parent catalog nodes/categories the product has. 

What is the suggested way to accomplish this? Should I create a complex object and index those to Find, or is there another more straightforward way?

Br,
Keijo

#181727
Aug 29, 2017 9:25
Vote:
 

Hey Keijo,

By default products in Find will have a hierarchy of all their categories stored against the document.  This is available through the Ancestors property. The example below uses a node as the currentContent and retrieves all products within that. Is that what you're looking for?

SearchClient.Instance.Search<ProductContent>()
                        .Filter(
                           p =>
                               p.Ancestors()
                                   .Match(currentContent.ContentLink.ToReferenceWithoutVersion().ToString()))
#181747
Aug 29, 2017 11:26
Vote:
 

Thanks Janaka, that can help me to filter the results.

I'm looking for solution to create facets based on nodes the product has. Something like the documentation instructs here (below "Grouping authors"): 

https://world.episerver.com/documentation/developer-guides/find/NET-Client-API/searching/Facets/Terms-facets/

var searchResults = client.Search<Book>()

    .TermsFacetFor(x => x.Author.Name)
    .Take(0)
    .GetResult();

var authorCounts = searchResults
    .TermsFacetFor(x => x.[THIS SHOULD BE THE LIST OF PARENT NODES?]).Terms;
foreach(var authorCount in authorCounts)
{
    string authorName = authorCount.Term;
    int count = authorCount.Count;
    Console.WriteLine(authorName + ": " + count);
}

I'm now thinking that maybe I should create a property for model that gets the list of parent nodes?

#181753
Aug 29, 2017 12:03
Vote:
 

Hi Keijo,

Terms facets would be the right option here.

It depends on the requirements but normally what I would do is probably have two string properties on the product which put it into a top-level category and subcategory. The properties are not visible to editors. These would be the names of the categories obtained through a helper class when indexing.  

Then I can simply do terms faceting on these fields in my search.

#181758
Aug 29, 2017 12:34
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.