
Before you begin
This tutorial uses Rules and also requires InstantSearch.js to be installed.Implementation guide
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)
File and index structure
For this implementation, you need two files:index.html
src/app.js
related_categories_example
.
Create the rule
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 an API client. 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:
JSON
JSON
Update the HTML
In yourindex.html
file, add the following containers.
HTML
Update the JavaScript
In yourapp.js
file, use the addWidgets
method to add four widgets to the page: searchBox
, hits
, refinementList
, and rangeInput
.
JavaScript
queryRuleContext
widget.
JavaScript
queryRuleCustomData
widget to parse the rule’s response and render the filter suggestions.
JavaScript
JavaScript