Skip to main content
Signature
<ais-query-rule-context
  :tracked-filters="object"
  // Optional parameters
  :transform-rule-contexts="function"
/>

Import

  • Component
  • Plugin
To ensure optimal bundle sizes, see Optimize build size.
Vue
import { AisQueryRuleContext } from "vue-instantsearch";
// Use "vue-instantsearch/vue3/es" for Vue 3

export default {
  components: {
    AisQueryRuleContext
  },
  // ...
};

About this widget

The ais-query-rule-context 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 contexts, so you can trigger context based rules on refinement.

Examples

Vue
<template>
  <ais-query-rule-context :tracked-filters="trackedFilters" />
</template>

<script>
export default {
  data() {
    return {
      trackedFilters: {
        genre: () => ["Comedy", "Thriller"],
        rating: (values) => values,
      },
    };
  },
};
</script>

Props

tracked-filters
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 so that they only consist of alphanumeric characters, hyphens, and underscores. Non-matching characters are replaced with underscores.
Vue
<template>
  <ais-query-rule-context :tracked-filters="trackedFilters" />
</template>

<script>
export default {
  data() {
    return {
      trackedFilters: {
        genre: () => ["Comedy", "Thriller"], // this tracks two static genre values
        rating: (values) => values, // this tracks all the rating values
      },
    };
  },
};
</script>
transform-rule-contexts
function
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.
Vue
<template>
  <ais-query-rule-context
    [...]
    :transform-rule-contexts="transformRuleContexts"
  />
</template>

<script>
export default {
  data() {
    return {
      transformRuleContexts: (ruleContexts) =>
        ruleContexts.map((ruleContext) =>
          ruleContext.replace("ais-", "custom-")
        ),
    };
  },
};
</script>
I