Use 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 is a context

A context is a string passed as a search parameter. In this tutorial, we illustrate 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. We can 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.

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],
});

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
10
11
12
13
14
15
const index = client.initIndex('movies');

const platformTag = getPlatform();

index.search(
  {
    query: 'query string',
    ruleContexts: platformTag
  },
  (err, { hits } = {}) => {
    if (err) throw err;

    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, which is not 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 record above, the release_date attribute is a Unix timestamp, which makes it suitable for for sorting and filtering.

Using the API

You can create a context-dependent Rule using the save-rule method of any of our API clients. Continuing on the example above, 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',
    'condition' => array(
        'context' => 'mobile',
    ),
    'consequence' => array(
        'params' => array(
            'filters' => 'release_date >= 1568498400',
        )
    )
);

$movies->saveRule($rule);

Using the dashboard

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

  1. Select the movies index that you plan to add Rules to.
  2. Select the Query Rules tab and click on the New Rule button.
  3. Under the Set up a trigger for your Rule, in the left sidebar, select the Target a specific context checkbox. In the input field, enter “mobile”.
  4. Under the Restrict results with filters button, click the Add filters button.
  5. Select release_date as your target attribute, then >= from the first dropdown (the one that defaults to is).
  6. Decide how recently films must be released to have their ranking increased. This value should be the Unix timestamp for around “one month ago”.
  7. You can ignore the Score input field. If you want to know more about what it means, check out how filter scoring works.
  8. Once you’ve filled in the required information, click the Update Filter button.
  9. If there are any films you want to pin to a specific location or rank at the top, you can click on the Select the order of individual results button in the sidebar.
  10. If you want to reorder top results, you can drag them around the image grid.
  11. Don’t forget to press the Save button at the bottom of the left sidebar.

Did you find this page helpful?