
Before you begin
This tutorial requires InstantSearch.js or React InstantSearch.Implementation guide
This solution follows a two-step process.Create a rule to detect facet values from the query
To filter on thecategory
attribute,
you first need to set it in attributesForFaceting
.
To learn more, see:
You can then create your rule: if a user types a term that’s one of the facet values of the category
attribute,
then automatically filter the query on that value.
You can create the rule from the Algolia dashboard or with an API client.
With the dashboard
To create this rule in the dashboard, follow these steps:- Go to the Algolia dashboard and select your Algolia application.
- On the left sidebar, select Search.
- Select your Algolia index and go to the Rules page.
- Select Create your first rule or New rule and select Manual Editor.
-
In the Condition(s) section, click Query contains, search for “categories”, and select
{facet:categories}
. -
In the Consequence(s) section set the following consequence:
- Choose Filter/Boost Matching Attributes.
- Search for “categories” and select the facet “categories”. It appears as Add Facet “categories”.
With an API client
If you are using one of the API clients, you should create thesaveRule
with the following JSON.
JSON
Automate refinement selection on the frontend
This solution uses a standard InstantSearch implementation. To automatically select refinements based on a rule, you need to:- Retrieve information from the rule you created earlier to select the refinements.
- Automatically select the onscreen refinements.
Source code
Retrieve active rule information
To retrieve data on the applied rule, you can use theexplain
query parameter in the configure
widget.
Sending explain: ['*']
with a query tells the engine to add extra rule details to the explain
attribute of the response.
These details help to debug or, as here, implement specific frontend logic.
JSON
Automatically select the relevant facet
You can automatically select the relevant facets by transforming the items in therefinementList
widget.
You can retrieve information about the rule-generated facet in the search response and use it to transform the matching item and mark it as refined.
When using React InstantSearch, you can retrieve information about the rule-generated facet in the search response with
useInstantSearch()
. You can do this in a React component that wraps <RefinementList>
and replaces it in your implementation.