Using rules to customize search results by platform
On this page
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 the user’s device.
To do this:
- Create and pass a
mobile
context string with user searches from mobile devices. - Create a contextual rule that acts on the
mobile
context.
What’s a context
A context is a string passed as a search parameter to InstantSearch or an Algolia API client.
Identifying your user’s device
Before you can assign a mobile
context to searches, you must identify your user’s device. To do this, 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 with InstantSearch
To set the user’s device at the beginning of their search session, 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],
});
Assign context with an API client
Pass the user’s device 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);
});
Create a contextual rule
After you’ve implemented and tested your app with the mobile
context, you can create a mobile rule (with an API client or from the dashboard).
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 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.
With an API client
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);
With the dashboard
With the dashboard, create a rule as follows:
- Select the Search product icon on your dashboard.
- Select the Rules section from the left sidebar menu in the Algolia dashboard.
- Under the heading Rules, select the index to which you’re adding a rule.
- Select Create your first rule or New rule. In the drop-down menu, click the Manual Editor option.
- In the Condition(s) section, toggle Context on and toggle Query off.
- Enter “mobile” in the context input field.
- In the Consequence(s) sections, click Add Consequence and select the Add Query Parameter consequence.
-
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" }
- 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,
- Save your changes.