queryRuleContext
instantsearch.widgets.queryRuleContext({ trackedFilters: object, // Optional parameters transformRuleContexts: function, });
About this widget
The queryRuleContext
widget lets you apply ruleContexts
based on filters to trigger context dependent Rules.
Rules offer a custom experience based on contexts. You might want to customize the users’ experience based on the filters of the search (for example, they’re visiting the “Mobile” category, they selected the “Thriller” genre, etc.) This widget lets you map these filters to their associated Rule contexts, so you can trigger context based Rules on refinement.
Examples
1
2
3
4
5
6
instantsearch.widgets.queryRuleContext({
trackedFilters: {
genre: () => ['Comedy', 'Thriller'],
rating: values => values,
},
});
Options
trackedFilters
|
type: object
Required
The filters to track to trigger Rule contexts. Each filter is a function which name is the attribute you want to track. They receive their current refinements as arguments. You can either compute the filters you want to track based on those, or return static values. When the tracked values are refined, it toggles the associated Rule contexts. The added Rule contexts follow the format Values are escaped to only consist of alphanumeric characters, hyphens, and underscores. |
||
Copy
|
|||
transformRuleContexts
|
type: function
Optional
A function to apply to the Rule contexts before sending them to Algolia. This is useful to rename Rule contexts that follow a different naming convention. |
||
Copy
|
Customize the UI with connectQueryRules
If you want to create your own UI of the queryRuleContext
widget, you can use connectors.
This connector is also used to build other widgets: QueryRuleCustomData
It’s a 3-step process:
// 1. Create a render function
const renderQueryRuleContext = (renderOptions, isFirstRender) => {
// Rendering logic
};
// 2. Create the custom widget
const customQueryRuleContext = instantsearch.connectors.connectQueryRules(
renderQueryRuleContext
);
// 3. Instantiate
search.addWidgets([
customQueryRuleContext({
// instance params
})
]);
Create a render function
This rendering function is called before the first search (init
lifecycle step)
and each time results come back from Algolia (render
lifecycle step).
const renderQueryRuleContext = (renderOptions, isFirstRender) => {
const {
object widgetParams,
} = renderOptions;
if (isFirstRender) {
// Do some initial rendering and bind events
}
// Render the widget
}
Rendering options
widgetParams
|
type: object
All original widget options forwarded to the render function. |
||
Copy
|
Create and instantiate the custom widget
We first create custom widgets from our rendering function, then we instantiate them. When doing that, there are two types of parameters you can give:
- Instance parameters: they are predefined parameters that you can use to configure the behavior of Algolia.
- Your own parameters: to make the custom widget generic.
Both instance and custom parameters are available in connector.widgetParams
, inside the renderFunction
.
const customQueryRuleContext = instantsearch.connectors.connectQueryRules(
renderQueryRuleContext
);
search.addWidgets([
customQueryRuleContext({
// Optional parameters
trackedFilters: object,
transformRuleContexts: function,
})
]);
Instance options
trackedFilters
|
type: object
Optional
The filters to track to trigger Rule contexts. Each filter is a function which name is the attribute you want to track. They receive their current refinements as arguments. You can either compute the filters you want to track based on those, or return static values. When the tracked values are refined, it toggles the associated Rule contexts. The added Rule contexts follow the format Values are escaped to only consist of alphanumeric characters, hyphens, and underscores. |
||
Copy
|
|||
transformRuleContexts
|
type: function
Optional
A function to apply to the Rule contexts before sending them to Algolia. This is useful to rename Rule contexts that follow a different naming convention. |
||
Copy
|
Full example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Create the render function
const renderQueryRuleContext = (renderOptions, isFirstRender) => {
// We don't provide anything to render for this widget.
};
// Create the custom widget
const customQueryRuleContext = instantsearch.connectors.connectQueryRules(
renderQueryRuleContext
);
// Instantiate the custom widget
search.addWidgets([
customQueryRuleContext({
trackedFilters: {
genre: () => ['Comedy', 'Thriller'],
},
})
]);