🎉 Try the public beta of the new docs site at algolia.com/doc-beta! 🎉
UI libraries / InstantSearch.js / Widgets
Signature
queryRuleContext({
  trackedFilters: object,
  // Optional parameters
  transformRuleContexts: function,
});
Import
1
import { queryRuleContext } from 'instantsearch.js/es/widgets';

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
queryRuleContext({
  trackedFilters: {
    genre: () => ['Comedy', 'Thriller'],
    rating: values => values,
  },
});

Options

Parameter Description
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 ais-{attribute}-{value} (for example ais-genre-Thriller). If the context of your Rule follows another format, you can specify it using the transformRuleContexts option.

Values are escaped to only consist of alphanumeric characters, hyphens, and underscores.

1
2
3
4
5
6
queryRuleContext({
  trackedFilters: {
    genre: () => ['Comedy', 'Thriller'], // this tracks two static genre values,
    rating: values => values, // this tracks all the rating values
  },
});
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.

1
2
3
4
5
6
7
queryRuleContext({
  // ...
  transformRuleContexts(ruleContexts) {
    return ruleContexts
      .map(ruleContext => ruleContext.replace('ais-', 'custom-'));
  },
});

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

To use connectQueryRules, you can import it with the declaration relevant to how you installed InstantSearch.js.

1
import { connectQueryRules } from 'instantsearch.js/es/connectors';

Then it’s a 3-step process:

// 1. Create a render function
const renderQueryRuleContext = (renderOptions, isFirstRender) => {
  // Rendering logic
};

// 2. Create the custom widget
const customQueryRuleContext = 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

Parameter Description
widgetParams
type: object

All original widget options forwarded to the render function.

1
2
3
4
5
6
7
8
9
10
11
12
13
const renderQueryRuleContext = (renderOptions, isFirstRender) => {
  const { widgetParams } = renderOptions;

  widgetParams.container.innerHTML = '...';
};

// ...

search.addWidgets([
  customQueryRuleContext({
    container: document.querySelector('#queryRuleContext'),
  })
]);

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 = connectQueryRules(
  renderQueryRuleContext
);

search.addWidgets([
  customQueryRuleContext({
    // Optional parameters
    trackedFilters: object,
    transformRuleContexts: function,
  })
]);

Instance options

Parameter Description
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 ais-{attribute}-{value} (for example ais-genre-Thriller). If the context of your Rule follows another format, you can specify it using the transformRuleContexts option.

Values are escaped to only consist of alphanumeric characters, hyphens, and underscores.

1
2
3
4
5
6
customQueryRuleContext({
  trackedFilters: {
    genre: () => ['Comedy', 'Thriller'], // this tracks two static genre values, 
    rating: values => values, // this tracks all the rating values
  },
});
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.

1
2
3
4
5
6
7
customQueryRuleContext({
  // ...
  transformRuleContexts(ruleContexts) {
    return ruleContexts
      .map(ruleContext => ruleContext.replace('ais-', 'custom-'));
  },
});

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 = connectQueryRules(
  renderQueryRuleContext
);

// Instantiate the custom widget
search.addWidgets([
  customQueryRuleContext({
    trackedFilters: {
      genre: () => ['Comedy', 'Thriller'],
    },
  })
]);
Did you find this page helpful?