Dynamic Faceting with Solr
A precise product search and the ability to refine search results using facets are integral parts of any online shop.
Facets are often used across hundreds of different attributes, but it is not efficient — and sometimes simply impossible — to calculate all the facets for each and every search.
Often, the solution to this problem is to manually maintain facets on the category tree. Facets that are valid across all products, such as price, categories, and brands, are maintained at the root category, while facets for products of a specific category are configured further down the category tree.
This is not only tedious to maintain, but it also does not work very well for full-text searches where no category has been selected in advance. Such a search returns results across a wide range of different products. For example, a search for “usb cable” on an electronics store returns results across very different types of products, leading to very messy facets.
Wouldn't it be nice if the facets applied to a search were tailored to each individual search, rather than relying on any static context such as the category I am searching within? If an attribute is present in a high percentage of the products within the search, we would want to use it as a facet; if an attribute is sparse, we would not.
This approach not only eliminates the need to manually maintain rules about which facet is applied, it also ensures that only facets relevant to each individual search are calculated and displayed.
With Solr, you can make this happen using only two custom classes and around 100 lines of code. In the GitHub repository below, I explain the technical implementation and provide all the code required.