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 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:
- Creating and passing a
mobile
context with user searches. - 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.
Assigning a context to a search
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
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, 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 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',
'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:
- Select the Rules section from the left sidebar menu in the Algolia dashboard.
- Under the heading Rules, select the index you are adding a Rule to.
- Select Create your first rule or New rule. In the dropdown, click on 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 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).Copy1 2 3
{ "filters": "release_date >= 1577836800" }
- 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.
- Don’t forget to save your changes.