🎉 Try the public beta of the new docs site at algolia.com/doc-beta! 🎉
Guides / Managing results / Rules / Merchandising

Search isn’t only about retrieving results. Sometimes, depending on what the user searches for, you may want to display custom data in your UI, like ads or promotional banners.

For example, consider an online bookstore that wants to display a discount for users looking for Harry Potter titles. With Algolia’s Rules, they can return custom data for specific queries and use it to display a banner.

Creating a rule

If you want to motivate users to buy Harry Potter books, you could display a special promotion banner at the top of search results whenever their search query contains “harry potter”: “20% OFF on all Harry Potter books!”

For this, first need to set a rule that returns the custom data you want to display. Then, you need to display this data whenever it comes up.

You can add the rule using the API or the Algolia dashboard. Then, you can activate it in your frontend code to retrieve the banner.

Using the API

To add a rule, you need to use the saveRule method. When setting a Rule, you need to define a condition and a consequence.

For the preceding example, you want to show the banner whenever the query contains “harry potter”. You don’t need it to be exact.

If the query is “harry potter azkaban” or “books harry potter”, you still want to display the promotion.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$rule = array(
  'objectID' => 'harry-potter-rule',
  'condition' => array(
    'pattern'   => 'harry potter',
    'anchoring' => 'contains',
  ),
  'consequence' => array(
    'userData' => array(
      'promo_content' => '20% OFF on all Harry Potter books!'
    )
  )
);

$response = $index->saveRule($rule);

Using the dashboard

You can also add your Rules in your Algolia dashboard.

  1. Select the Search product icon on your dashboard and then select your index.
  2. Select the Rules section from the left sidebar menu in the Algolia dashboard.
  3. Under the heading Rules, select the index you are adding a Rule to.
  4. Select Create your first rule or New rule. In the drop-down menu, click the Manual Editor option.
  5. In the Condition(s) section, keep Query toggled on, select Contains from the drop-down menu, and enter “harry potter” in the input field.
  6. In the Consequence(s) section:
  • Click the Add consequence button and select Return Custom Data.
  • In the input field that appears, add the data to return when the user query matches the Rule: { "promo_content": "20% OFF on all Harry Potter books!" }
  1. Save your changes.

Determining priority when you have multiple banners

You may have quite a few semi-permanent promotional banners for specific product categories, as well as some time-limited, temporary banners for certain products.

For instance, an online IT hardware store has banners for the following categories:

  • Phones
  • Tablet computers
  • Laptop computers
  • Desktop computers.

They also have temporary offers and associated banners for the phrases/products:

  • Huawei Honor Note 10
  • Xiaomi Redmi Note 10 Pro
  • Samsung Galaxy Note 20 Ultra
  • Samsung Galaxy Tab S7+
  • Samsung Galaxy Book Pro 360.

Since Algolia can only apply one Rule at a time (and, hence, display one banner), tie-breaking precedence logic is used to determine which one to use.

Using the preceding example, the user starts on the Phones page (and can see the appropriate banner for that), then searches for “note”. For most stages, Huawei Honor Note 10, Xiaomi Redmi Note 10 Pro, and Samsung Galaxy Note 20 Ultra are tied and take precedence over the category banners. The final tie-breaking stage, invoked in this case, is Rule ID: this looks for the Rule with the smallest objectID, which in this case just happens to be for the Huawei Honor Note 10.

Don’t assume that a Rule that was created some time ago will have a smaller objectID than a more recent Rule. It’s best to not rely on objectID to determine precedence but to manually set it.

Duplicate Rules to adapt to user search patterns

To ensure that the Samsung Galaxy Note 20 Ultra banner displays for the search term “note” in the preceding example:

  1. Duplicate the existing Samsung Galaxy Note 20 Ultra Rule.
  2. Edit the Rule to change the Query search phrase to “Note”.

The newly duplicated Rule will now match the first precedence logic criterion (Position) and display the desired banner.

Retrieving the banner data in the Search UI

Now that your Rule is ready, you can add a banner in your search UI when the userData property is present in the API response. Here’s what it could look like with InstantSearch:

1
2
3
4
5
6
7
8
9
10
11
12
search.addWidgets([
  instantsearch.widgets.queryRuleCustomData({
    container: '#banner',
    templates: {
      default: `
        {{#items}}
          <p>{{promo_content}}</p>
        {{/items}}
      `,
    },
  })
]);

You can learn more about this widget in the InstantSearch API reference:

Applying rules with context conditions

If you want to apply Rules based on filters, have a look at:

Did you find this page helpful?