Guides / Managing results / Rules / Rules overview

Using Rules to Customize Search Results By Platform

Search needs to be flexible. Specific situations, independent of a user’s query, call for different results. For example, because certain products are more likely bought or viewed from phones than desktops, you may want to promote items based on user platform.

To do this, you need to create a Rule that depends on the mobile context. This guide takes you through the two main steps of the process:

  1. Creating and passing a mobile context with user searches.
  2. Creating a Rule that acts on the mobile context.

What’s a context

A context is a string passed as a search parameter. This tutorial illustrates two ways of creating a mobile context: with InstantSearch and with the API clients.

Identifying your user’s device

Before assigning a mobile context to searches, you need to identify your user’s device. To do this, write a function that detects what device it’s running on, and returns “desktop” or “mobile” depending on what it finds:

1
2
3
4
5
const getPlatform = () => {
  const isMobile = ...
  // your logic to determine whether a user is on mobile or desktop
  return isMobile ? 'mobile' : 'desktop';
};

Fetching your user’s device makes more sense on the front end, which is why there are no back-end snippets.

Assigning context with InstantSearch

To set the user’s device at the beginning of their search session, you can add it to the searchParameters setting of your InstantSearch instance during initialization.

1
2
3
4
5
const platformTag = getPlatform();

instantsearch.widgets.configure({
  ruleContexts: [platformTag],
});

Assigning context with an API client

You can also pass the user’s device using the API clients, by providing it as a search parameter whenever you call search on your index.

1
2
3
4
5
6
7
8
9
const index = client.initIndex('movies');

const platformTag = getPlatform();

index.search('query', {
  ruleContexts: platformTag
}).then(({ hits }) => {
  console.log(hits);
});

Creating contextual Rules

After you’ve implemented and tested your app with the mobile context, you can create your mobile rule.

Suppose that you are a movie database company. After some research, you realize that mobile users often go see a film in theaters if it shows up in their search results. This isn’t the case for desktop users.

You set up a partnership with a movie theater chain, and decide to promote recently released movies to your mobile users. The records in your Algolia index have the following attributes:

1
2
3
4
5
6
7
{
  "title": "Fargo",
  "year": 1996,
  "director": "Joel Coen",
  "release_date": 841795200,
  "rating": 93
}

In the example record, the release_date attribute is a Unix timestamp, which makes it suitable for sorting and filtering.

Using the API

You can create a context-dependent Rule using the save-rule method. Continuing with the example, you would call save-rule with the following parameters:

1
2
3
4
5
6
7
8
9
10
11
12
13
$rule = array(
    'objectID' => 'a-rule-id',
    'conditions' => array(array(
        'context' => 'mobile',
    )),
    'consequence' => array(
        'params' => array(
            'filters' => 'release_date >= 1577836800',
        )
    )
);

$movies->saveRule($rule);

Using the dashboard

With the dashboard, you can test and create Rules. Follow the steps below:

  1. Select the Search product icon on your dashboard.
  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 dropdown, click on the Manual Editor option.
  5. In the Condition(s) section, toggle Context on and toggle Query off.
  6. Enter “mobile” in the context input field.
  7. In the Consequence(s) sections, click Add Consequence, and select the Add Query Parameter consequence.
  8. In the editor that appears, add a filter for the release_date attribute. In the following example, we filter all movies with a release date in 2020 (1577836800 is the Unix timestamp for January 1st, 2020).
    1
    2
    3
    
    {
      "filters": "release_date >= 1577836800"
    }
    
  9. If there are films that you want to pin to a specific location, or rank at the top, you can add other consequences by clicking Add consequence.
  10. Don’t forget to save your changes.
Did you find this page helpful?