API Reference / InstantSearch.js Widgets / queryRuleContext
Apr. 24, 2019

queryRuleContext

Widget signature
instantsearch.widgets.queryRuleContext({
  trackedFilters: object,
  // Optional parameters
  transformRuleContexts: function,
});

About this widget

The queryRuleContext widget lets you apply ruleContexts based on filters to trigger contextual Query Rules.

Query Rules offer a custom experience based on rule contexts. You might want to customize the users’ experience based on the filters of the search (e.g., they’re visiting the “Mobile” category, they selected the “Thriller” genre, etc.) This widget lets you map these filters with their associated rule contexts, so you can trigger contextual Query Rules upon 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 ais-{attribute}-{value} (e.g. 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
instantsearch.widgets.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
instantsearch.widgets.queryRuleContext({
  // ...
  transformRuleContexts(ruleContexts) {
    return ruleContexts
      .map(ruleContext => ruleContext.replace('ais-', 'custom-'));
  },
});

Customize the UI - 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.addWidget(
  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.

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

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

// ...

search.addWidget(
  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 = instantsearch.connectors.connectQueryRules(
  renderQueryRuleContext
);

search.addWidget(
  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 ais-{attribute}-{value} (e.g. 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 = instantsearch.connectors.connectQueryRules(
  renderQueryRuleContext
);

// Instantiate the custom widget
search.addWidget(
  customQueryRuleContext({
    trackedFilters: {
      genre: () => ['Comedy', 'Thriller'],
    },
  })
);

Did you find this page helpful?