- Positive filters include a specific subset of matching records in the results. For example, if a user types “diet” on a restaurant website, return every record that has “low-carb” or “low-fat”.
- Negative filters exclude matching records from results. For example, if a user types “gluten-free” on a restaurant website, you could filter out any gluten-containing meal.
- Numerical filters convert text queries into a numerical range. For example, if a user types “cheap” on a website for kitchen appliances, you could filter out anything costing more than $50.
Positive filters
If you want to filter out every non-diet-friendly meal whenever user’s search queries contain the term “diet”, you could use the_tags attribute to categorize meals depending on their individual qualities:
JSON
_tags attribute.
Because _tags is already optimized for filtering, you don’t have to set it as an attribute for faceting.
You can directly create a new Rule that detects the term “diet” in a query and applies a positive filter on tags “low-carb” and “low-fat”.
Using the API
To run the code examples on this page, install the latest API client. To add a rule, use thesaveRule method.
When creating a rule, you must define a condition and a consequence.
Using the dashboard
You can add rules from the Algolia dashboard using either the Visual Editor or the Manual Editor.- Visual Editor
- Manual Editor
- In the Algolia dashboard, select your application and index, open Rules, and select New rule > Visual Editor.
- Click Set query condition(s), select contains, and enter
diet. Click Apply. - In Consequences, select Filter results.
Select
_tagsas the attribute andlow-carbas the value. Addlow-fatas another value and join the values with Or. Click Apply. - Click Add Consequence, expand More consequences, and select Remove word.
Enter
dietin Word(s) and click Apply. - Review the results, click Review and Publish, and click Publish.
Negative filters
To exclude gluten-containing foods from the search results when a user searches for gluten-free meals, do the following:- Create an
allergensattribute (with “gluten” as one of the potential values). - Create a rule that filters out records with “gluten” in that attribute.
Example records
JSON
Using the API
Setallergens as an attributesForFaceting in your index:
- Use the
saveRulemethod to create a rule that detects the term “gluten-free” in a query and applies a negative filter on facet valueallergens:gluten. - Add a consequence in your rule to remove the word “gluten-free” from your query. This way, it won’t be used as a search term, only for filtering purposes.
Using the dashboard
You can add rules from the Algolia dashboard using either the Visual Editor or the Manual Editor. Before creating the rule, open Configuration > Filtering and Faceting > Facets for your index and add theallergens attribute.
- Visual Editor
- Manual Editor
- In the Algolia dashboard, select your application and index, open Rules, and select New rule > Visual Editor.
- Click Set query condition(s), select contains, and enter
gluten-free. Click Apply. - In Consequences, select Filter results.
Select
allergensas the attribute, is not as the operator, andglutenas the value. Click Apply. - Click Add Consequence, expand More consequences, and select Remove word.
Enter
gluten-freein Word(s) and click Apply. - Review the results, click Review and Publish, and click Publish.
Numerical filters
Consider the query “cheap toaster 800w”. You can use Rules to filter the results by “toaster” and “prices under $10” so that the only textual search is the remaining term, “800w”, which could further be used to limit the results with that wattage.Rule
Ifquery = "cheap toaster" then price < 10 and product_type:toaster.
This requires two rules because each query term applies a different filter.
Using the API
Using the dashboard
You can configure this example from the Algolia dashboard using either the Visual Editor or the Manual Editor.Preparation
- Go to the Algolia dashboard and select your Algolia application.
- On the left sidebar, select Search.
- Select your Algolia index and open the Configuration tab.
- On the Filtering and Facets page,
click Add an attribute and select the
product_typeattribute. Repeat to addprice.
- Visual Editor
- Manual Editor
In the Visual Editor, set up separate rules for
toaster and cheap.- In the Algolia dashboard, select your application and index, open Rules, and select New rule > Visual Editor.
- Click Set query condition(s), select contains, and enter
toaster. Click Apply. - In Consequences, select Filter results.
Select
product_typeas the attribute andtoasteras the value. Click Apply. - Click Add Consequence, expand More consequences, and select Remove word.
Enter
toasterin Word(s), then click Apply. - Review the results, click Review and Publish, and click Publish.
cheap for the condition and removed word, and filter on price < 10.