javacommercetools

Is it possible to create faceted search in CT using product attributes?


I am able to create faceted search using variants' attributes:

.plusFacets(facets -> facets.allVariants().attribute().ofString(sizeDescriptionAttribute).allTerms())

But I haven't managed to perform the same if I wanted to do it using products' attributes mixed with variants' attributes.

Is it possible to perform such operation?


Solution

  • You can use a -> {} syntax to build more complex process. In example:

    .plusFacets(facets -> { 
        Object variants = facets.allVariants().attribute().ofString(sizeDescriptionAttribute).allTerms();
        Object products= searchForProducts(fecets);
        return mixOf(variants, products);
    }).doMoreStuff();
    

    So this whill give you a mix. You just need to implement missing methods or use similar syntax.