Facets are extracted from the search result. Once a customer selects an option from the facet, we create a filter query that is typically an AND
logic. For example, on searching for a particular item, say tommy hilfiger
, we would be getting results that will have facets for size and color. It was previously assumed that the customer would select a single option from both the facets. Say the selections are medium
for size and green
for color. The filter query would be:
fq=clothes_size:medium&fq=clothes_color:green
This will be appended to our search query:
q=tommy%20hilfiger&qf=text%20cat^2%20name^2%20brand^2%20clothes_type^2%20clothes_color^2%20clothes_occassion^2&pf=text%20cat^3%20name^3%20brand^3%20clothes_type^3%20clothes_color^3%20clothes_occassion^3&fl=name,clothes_size,clothes_color,score&defType=edismax&facet=true&facet.mincount=1&facet.field=clothes_gender&facet.field=clothes_type&facet.field=clothes_size&facet.field=clothes_color&facet.field=brand&facet.field=mobile_os&facet.field=mobile_screen_size&facet.field=laptop_processor&facet.field=laptop_memory&facet.field=laptop_hard_disk&fq=clothes_size:medium&fq=clothes_color:green
What happens if the customer intends to select multiple options in facets? In this case, suppose the customer is interested in both medium
and large
sizes and also in green
and red
colors. To handle this, we will have to use OR
between multiple options in our filter query for a particular field. The filter query in this case will be:
fq=clothes_size:medium clothes_size:large&fq=clothes_color:red clothes_color:green
The complete query will be:
q=tommy%20hilfiger&qf=text%20cat^2%20name^2%20brand^2%20clothes_type^2%20clothes_color^2%20clothes_occassion^2&pf=text%20cat^3%20name^3%20brand^3%20clothes_type^3%20clothes_color^3%20clothes_occassion^3&fl=name,clothes_size,clothes_color,score&defType=edismax&facet=true&facet.mincount=1&facet.field=clothes_gender&facet.field=clothes_type&facet.field=clothes_size&facet.field=clothes_color&facet.field=brand&facet.field=mobile_os&facet.field=mobile_screen_size&facet.field=laptop_processor&facet.field=laptop_memory&facet.field=laptop_hard_disk&fq=clothes_size:mediumclothes_size:large&fq=clothes_color:redclothes_color:green
The filter query is interpreted as follows:
Filter query for the multi-select option
The results would be as follows:
Multi-select search result
Another problem that plagues faceting on an e-commerce site is known as disappearing facets. To show only facets that are relevant to the search result, we set the facet.mincount
parameter to 1
. This ensures that only facets that contain certain values are shown. What if further filter queries reduce the facet count to 0
and make it disappear from the facets?
Let us take an example to understand the problem.
A search for tommy
hilfiger
gives us facets in size and color:
Facets for "tommy hilfiger" search
When we apply a filter query for size=small
, we intend to have an output specifying that there are no sweaters in red
matching the size we have selected:
size: small -> 1, medium -> 0, large -> 0 color: red -> 0, green -> 1
However, since the facet.mincount=1
parameter, the output is:
size: small -> 1 color: green -> 1
Disappearing facets
The way to handle this is to tag filter queries and exclude them from facets. The filter query can be tagged as follows:
fq={!tag=size_fq}clothes_size:small
The tagged filter query can be excluded from the facet using the following syntax:
facet.field={!ex=size_fq}clothes_size
In order to handle the disappearing facet scenario, we have created multiple facets on the same field—one facet bearing the exact count from the result set taking into consideration the filter queries and another facet that excludes all filter queries.
We have modified the preceding query to handle this scenario, and simplified the query to focus exactly on the modifications that we have implemented:
q=tommy%20hilfiger&qf=text%20cat^2%20name^2%20brand^2%20clothes_type^2%20clothes_color^2%20clothes_occassion^2&pf=text%20cat^3%20name^3%20brand^3%20clothes_type^3%20clothes_color^3%20clothes_occassion^3&fl=name,clothes_size,clothes_color,score&defType=edismax&facet=true&facet.mincount=1&facet.field=clothes_size&facet.field=clothes_color&facet.field={!ex=size_fq,color_fq key=all_size_facets}clothes_size&facet.field={!ex=size_fq,color_fq key=all_color_facets}clothes_color&fq={!tag=size_fq}clothes_size:small
Here, we have created multiple facets on both clothes_size
and clothes_color
fields, one in which filter queries are excluded and another in which they are not. Also, the filter queries are tagged as size_fq
. The facets in which filter queries are excluded are tagged as all_size_facets
and all_color_facets
. The following is the output from the query:
Both missing facets and all results facets seen here
Our program has to take care of merging both the facets for size and color and create the appropriate output. For example, the output can contain the facet counts from the all_clothes_color
tag. Except for green
, all others could be disabled (instead of displaying 0
).