Dynamic filtering with Rules
Dynamic filtering with Rules search.
To help users find what they’re looking for straight from the search box, you can use Algolia Rules to automatically apply a filter to results if the query matches a facet value.
How to create a dynamic filtering rule
In this example for a product catalog, you create a rule that displays products with the “red” value in the color
facet if the query contains the word “red”.
Using the example dataset, this rule ensures that only records with “red” in the color
attribute are returned. It ignores “red” in other attributes such as brand
.
Query | Results |
---|---|
red | FM, Clothing, LondonLook, Red, 21.99 The Mandal, Toaster, Black & Decker, Red, 149.99 |
Using the dashboard
-
Go to the Algolia dashboard and select your Algolia application.
-
On the left sidebar, select Search.
-
Select your Algolia index:
-
Click Rules in the left sidebar menu.
-
Select Create your first rule or New rule. In the drop-down menu, click the Manual Editor option.
-
In the Condition(s) section, with Query toggled on, select Contains in the drop-down menu, and enter “red” in the input field.
-
In the Consequence(s) section:
- Click Add consequence and select Add Query Parameter.
- In the input field that appears, add the JSON parameters you want to apply when the user’s query matches the Rule:
{"filters":"color:red"}
- Click Add consequence and select Remove Word.
- Type or select “red” in the input field.
If you click Optional under Consequence(s), this will display non-red products after the red ones. For more information, see Using optional filters.
- Save your changes.
Using the API
This example uses the filters
parameter.
With this approach, you need one rule per filter value.
If you have 10 color options, you need 10 rules, one for each color.
Examples
The following examples explore:
One rule per facet
Instead of creating one rule per facet value, this approach uses one rule for the color
facet.
Using the example dataset, this single rule ensures that any record with any matching color value in the color
attribute will be returned.
Query | Results |
---|---|
red | FM, Clothing, LondonLook, Red, 21.99 The Mandal, Toaster, Black & Decker, Red, 149.99 |
black | Will Carpenter, T-shirt, Red or Dead, Black, 199.99 |
Using the dashboard
- Create a new rule. See how to create a dynamic filtering rule.
- In the Manual Editor’s Condition(s), click the box next to Query contains and select the option Add Facet “color” from the drop-down menu.
- In the Consequence(s) section:
- Click Add consequence and select Filter/Boost Matching Attributes.
- Click the Filter box that appears and select the option Add Facet “color” from the drop-down menu.
- Save your changes.
Using the API
Numerical dynamic filtering
Consider a user entering the query “cheap toaster 800w”. You can use two rules together to filter results by:
- The product type: “Toaster”
- “Cheapness”. You determine that a cheap product is any product that costs less than $15.
Using the example dataset, these rules ensure that any product that costs less than $15, has the product_type
“Toaster” and includes the phrase “800w” is returned.
Query | Results |
---|---|
cheap toaster 800w | Essentials 800w, Toaster, Daewoo, Black, 14.99 |
Using the dashboard
Create the first rule:
- Create a new rule. See how to create a dynamic filtering rule.
- In the Manual Editor’s Condition(s), with Query toggled on, select Contains in the drop-down menu, and enter “toaster” in the input field.
- In the Consequence(s) section:
- Click Add consequence and select Add Query Parameter.
- In the input field that appears, enter
{"filters":"product_type:toaster"}
- Click Add consequence and select Remove Word.
- Type or select “toaster” in the input field.
- Save your changes.
Create the second rule:
- Create a new rule
- In the Manual Editor’s Condition(s), with Query toggled on, select Contains in the drop-down menu, and enter “cheap” in the input field.
- In the Consequence(s) section:
- Click Add consequence and select Add Query Parameter.
- In the input field that appears, enter
{"filters":"price < 10"}
- Click Add consequence and select Remove Word.
- Type or select “cheap” in the input field.
- Save your changes.
Using the API
Tagged filters
Consider a user who enters the query “apple headphones”.
They would expect to search for results that match the term “headphones”, but only where the brand
attribute matches “apple”.
You can find this behavior on sites such as GitHub.
It’s a great alternative to filters for users who prefer to type rather than click.
This approach is similar to one rule per facet.
Using the example dataset, this rule ensures that any record containing the phrase “headphones” that also has “Apple” in the brand
attribute will be returned. It won’t return non-Apple brands, even if the phrase “Apple” appears in other attributes.
Query | Results |
---|---|
apple headphones | Airpods Max, Headphones, Apple, Gray, 548.99 |
Using the dashboard
- Create a new rule as above
- In the Manual Editor’s Condition(s), click the box next to Query contains and select the option Add Facet “brand” from the drop-down menu.
- Clear the Apply to plurals, synonyms and typos checkbox to ensure precise matching of the brand name.
- In the Consequence(s) section:
- Click Add consequence and select Filter/Boost Matching Attributes.
- Click the Filter box that appears and select the option Add Facet “brand” from the drop-down menu.
- Click Add consequence and select Remove word.
- In the input that appears, select Remove
{facet:brand}
.
- Save your changes.
Using the API
Example dataset
All the examples on this page use a product catalog index. The appropriate attributes have been set as attributes for faceting. The index has records that look like this:
See also
Was this page helpful?