Help users access their desired content faster by displaying suggested filters based on their selected facets.
Facets are a great way to let your users refine their search, but it can be difficult for them to find the right ones if they see a long list of facets. A solution to this problem is to suggest facets based on your users’ already selected facets.In this image, and the demo,
you see three suggested filters above the search results after you click the Cell Phones facet:
iPhone Accessories, Audio, and Price < 100.
These extra filters guide help users refine and improve the relevance of their results.
The words “Add” and “Replace” give users helpful information about the effect of clicking on the .
For example, by clicking on iPhone Accessories - Replace,
this automatically selects the iPhone Accessories facet and deselects Cell Phones.
In this guide, you build a classic ecommerce search page with a search box, two facets (category and price), and results. The interesting bit is that when your user selects the category ‘phone’, you suggest two filters: category: phone accessories and price <= 100.Implement this behavior as follows:
Create the rules
Display the filter suggestions (HTML)
Retrieve and render the filter suggestions (JavaScript)
First, define the filters you want to suggest for each category.
The most flexible way to do this is with Rules,
which you can create from the dashboard or with one of the API clients.Rules consist of two main parts: a condition that triggers the rule, and a consequence that slightly alters the API response.To create this rule, follow these steps:
Select the Rules section from the left sidebar menu in the Algolia dashboard.
Under the heading Rules, select the index to which you’re adding a rule.
Select Create your first rule or New rule. In the drop-down menu, click the Manual Editor option.
In the Condition(s) section, set the context to ais-categories-Cell_Phones (you set this context on the frontend when your user clicks on the ‘phone’ category).
In the Consequence(s) section set the following consequence:
Click the Add consequence button and select Return Custom Data.
In the input field that appears, copy and paste the JSON text below:
Now, you have to set the rule context whenever your user selects a facet so that it changes when they select the ‘phone’ category. You can do this by adding the queryRuleContext widget.
With this solution in place, you can add new suggestions of filters by creating new rules, without any extra coding.
This guides your users to even more relevant results.To learn more, see: