Guides / Managing results / Rules / Rules overview

Using rules to customize search results by device

Search needs to be flexible. Depending on the user’s context, results can vary. For example, because some products are more likely to be bought or viewed from phones than desktops, you may want to promote items based on the user’s device.

To do this:

  1. Identify your user’s device.
  2. Assign a mobile context when users search from mobile devices. A context is a string passed as a search parameter to InstantSearch or an Algolia API client.
  3. Create a contextual rule that acts on the mobile context.

To help understand user behavior across devices, pair your device-specific experience with analytics. You can even use the same context string to tag search queries for analytics. These analyticsTags help you group search analytics into different segments.

Example scenario

Suppose you work for a movie database company. Research shows that mobile users, unlike desktop users, tend to see a film in theaters after it has appeared in their search results. To capitalize on this, you partner with a movie theater chain and decide to promote newly released movies to your mobile users.

The records in your Algolia index include a release_date attribute, in Unix timestamp format, this is useful for identifying newly released movies.

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

By creating a mobile context and contextual rules, you can promote recent movies to mobile users.

Identify your user’s device

Before assigning a mobile context to searches, write a function that detects what device the app is running on, and return “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 a mobile or desktop device
  return isMobile ? 'mobile' : 'desktop';
};

Assign context

You can assign context with InstantSearch or an Algolia API client.

Assign context with InstantSearch

Set the user’s device context at the beginning of their search session by adding it to the searchParameters setting of your InstantSearch instance during initialization.

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

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

Assign context with an API client

Pass the user’s device context as a search parameter whenever you search 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);
});

Create a contextual rule

You can create a mobile contextual rule with an API client or from the Algolia dashboard.

Create a contextual rule with an API client

Use the save-rule method to create a context-dependent rule. For example, to promote movies released after January 1, 2020, for mobile users:

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);

Create a contextual rule with the dashboard

  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 to which you’re adding a rule.
  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, 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, add a filter for the release_date attribute. For example, to show all movies released since 2020 (1577836800 is the Unix timestamp for January 1, 2020), enter: { "filters": "release_date >= 1577836800" }

  9. If you wish, you can add other consequences by clicking Add consequence. For example, there may be movies that you want to pin to a specific location or rank at the top,
  10. Save your changes.
Did you find this page helpful?