This feature isn’t available on every plan.
Refer to your pricing plan to see if it’s included.
Update your records
Since optional filters only work with exact matches, you might need to update your records. Using the fast food delivery website as an example, flag restaurants that can deliver within 20 minutes by adding a boolean attribute calledcan_deliver_quickly
and set it to true or false for each record.
JSON
How to apply optional filters
To use optional filters, you need to:- Set the attribute in
attributesForFacetingat indexing time. You can do this either through the API or the Algolia dashboard. - Add the optional filter through a rule or send it with your query using the API.
Using the dashboard
Set the attribute to use for faceting in your Algolia dashboard:- Go to the Algolia dashboard and select your Algolia .
- On the left sidebar, select Search.
- Select your Algolia index and go to the Configuration tab.
- Under Filtering and Faceting > Facets, click Add an attribute and select the optional filter attributes (for example,
can_deliver_quickly). - Save your changes.
can_deliver_quickly is true using either the Visual Editor or the Manual Editor.
- Visual Editor
- Manual Editor
- Open Rules and select New rule > Visual Editor.
- In Conditions, select Create conditionless rule and click Apply to apply the boost to every search. To limit the boost to specific queries, select Set query condition(s) instead.
- In Consequences, select Boost categories.
Select
can_deliver_quicklyas the attribute andtrueas its value, then click Apply. - Preview the results with an empty query, click Review and Publish, and click Publish.
can_deliver_quickly is true, while the other record remains in the results.
To demote records instead, use Bury categories in the Visual Editor.
For example, select can_deliver_quickly as the attribute and false as its value.
In the Manual Editor, use Add Query Parameter with { "optionalFilters": ["can_deliver_quickly:-false"] } for the same effect.
Using the API
To run the code examples on this page, install the latest API client. Set the attribute to use for faceting withattributesForFaceting.
For example:
Optional or facet filters
Whether to use optional filters or facet filters depends on your goals:- Use optional filters to boost records.
- Use facet filters if you need to filter out records.