Promote records with optional filters
On this page
Optional filters are a powerful tool for influencing the ranking of search results. Use them to boost or bury records based on specific criteria, but without hiding those records.
For example, on a website that handles fast food deliveries, if a user types “hungry” and clicks the “deliver quickly” option, the website will show the restaurants that can deliver the fastest at the top of the list but still show other restaurants.
You can also use negative optional filters to demote records. For example, to lower the ranking of restaurants with poor reviews.
Optional filters aren’t available on the Grow plan.
If you signed up for the Community, Essential, or Plus plans before December 15, 2018, you can only use one optional filter per query.
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 called can_deliver_quickly
and set it to true
or false
for each record.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[
{
"name": "Pasta Bolognese",
"restaurant": "Millbrook Deli",
"delivery_waiting_time": 20,
"can_deliver_quickly": true,
"popularity": 80
},
{
"name": "Pasta Bolognese",
"restaurant": "The Hive",
"delivery_waiting_time": 15,
"can_deliver_quickly": true,
"popularity": 80
},
{
"name": "Pasta Bolognese",
"restaurant": "Bert's Inn",
"delivery_waiting_time": 40,
"can_deliver_quickly": false,
"popularity": 90
}
]
How to apply optional filters
To use optional filters, you need to:
- Set the attribute as an
attributeForFaceting
at indexing time. You can do this either through the API or the Algolia dashboard. - Add the optional filter to your query with 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 application.
- On the left sidebar, select Search.
-
Select your Algolia index:
- Click the Configuration tab.
- In the Facets subsection of Filtering and Faceting, click the “Add an attribute” button and select the optional filter attributes from the drop-down menu (for example,
can_deliver_quickly
). - Save your changes.
You can test filters in the Rules Visual Editor before using them in your code:
- Go to the Algolia dashboard and select your Algolia application.
- On the left sidebar, select Search.
-
Select your Algolia index:
- Click Rules in the sidebar
- Create a new Rule and choose Visual Editor.
- Click the Set query condition(s) button under It all starts here.
- Click Filters, and in Filter Name, enter the name of your optional filter (for example,
can_deliver_quickly
) - In Value, type
true
- Type something into the Query box, such as
*
(to display all matching records) - Click Apply.
Using the example dataset and the query *
`, two out of three records will be displayed.
Using the API
Set the attribute to use for faceting with attributesForFaceting
.
For example:
1
2
3
4
5
6
$index->setSettings([
'attributesForFaceting' => [
"can_deliver_quickly",
"restaurant"
]
]);
Send the optional filters with your query. For example:
1
2
3
4
5
$results = $index->search("hungry", [
'optionalFilters' => [
"can_deliver_quickly:true"
]
]);
You can also add negative optional filters. For example, to demote a specific restaurant:
1
2
3
4
5
$results = $index->search("hungry", [
'optionalFilters' => [
"restaurant:-Bert's Inn"
]
]);
An alternative: facet filters
An alternative to optional filters is facet filters. To determine whether to use optional filters or facet filters, consider your specific goals:
- Use optional filters to boost records
- Use facet filters if you only need to filter out records.