Empty product search facet

36 Views Asked by At

Im working on a Spartacus/Composable Storefront project, and I need to show the product search facets.

But, i dont know where or why, my product facets on the FacetService is empty.

My /search endpoint is listing the active facets in response: All the loaded response from /search endpoint

but when i log the facetService.facetList$, like:

this.facetService.facetList$.subscribe((facetList) => {
    console.log(facetList);
});

Is logged:

activeFacets: []
facets: []

Curiously, the ProductListComponentService, when I try to log then, is logged this:

breadcrumbs: []
currentQuery: {query: {…}, url: '/search?q=aveeno%3Arelevance'}
facets: []
freeTextSearch: "aveeno"
pagination: {currentPage: 0, pageSize: 12, sort: 'relevance', totalPages: 1, totalResults: 3}
products: (3) [{…}, {…}, {…}]
sorts: (6) [{…}, {…}, {…}, {…}, {…}, {…}]
type: "productCategorySearchPageWsDTO"
[[Prototype]]: Object

The facets attribute is an empty array. Thats really strange, because i dont have any other place using this attribute all around my project.

Anyone know how to fix this? Thanks for your time.

I tried to debug the productListComponentService, no success.

2

There are 2 best solutions below

0
Kumar Rituraj On

Is there a custom implementation done? In standard facetservice calls ProductFacetService & it loads facets. Do check if any modification done in your landscape .

Regards KR

0
Mehmet S On

There will probably be no change in the results when you make your selection, so you cannot get results from facetService.facetList$

Because for the response from the search service, OccProductSearchPageNormalizer comes into play and some checks are made for the incoming facets array. If the count value for each facet in the response from the search service is less than the pagination.totalResults value, your facets will not be empty.

  protected normalizeUselessFacets(target: ProductSearchPage): void {
    if (target.facets) {
      target.facets = target.facets.filter((facet) => {
        return (
          !target.pagination ||
          !target.pagination.totalResults ||
          ((!facet.hasOwnProperty('visible') || facet.visible) &&
            facet.values &&
            facet.values.find((value) => {
              return (
                value.selected ||
                (value.count ?? 0) < (target.pagination?.totalResults ?? 0)
              );
            }))
        );
      });
    }
  }

Here, such a check has been made to prevent unnecessary requests to the API, but if you want to show these facets even if they will give the same results, you can customize the OccProductSearchPageNormalizer.

this: (value.count ?? 0) < (target.pagination?.totalResults ?? 0)

replace with: (value.count ?? 0) <= (target.pagination?.totalResults ?? 0)