A context controls whether a Rule should apply all the time, or only in some situations. For example, a context could control whether a particular Rule should apply to searches on mobile phones, but not on desktops.
Using a Rule context requires some coding.
Rules are global by default
Algolia offers a Rule mechanism that enables merchandising, intent detection, and other such dynamic behaviors. These Rules give you more control over search results. For example, you can create a merchandising Rule to ensure that the query “star wars” displays the most recent Star Wars film.
However, you may not want a Rule to apply in all situations. While Rules are global by default, some Rules don’t make sense in specific contexts. For example, certain products may sell better on mobile than desktops; therefore, you want a Rule that promotes these items based on the end user’s device.
Context limits the scope of a Rule
To offer contextual control, we’ve developed a context mechanism that enables you to define when a Rule applies.
Common use-cases for context are device (mobile, desktop, etc.), language (French, English, etc.), or country. Another typical context is category pages, such as pages dedicated to Apple products or Android phones.
How to create a context
Creating a context consist of two parts:
- Functionally: you need to carefully define contexts and give them meaningful labels, such as “device_mobile” or “category_page_apple”.
- Technically: your front-end code needs to add the context label to the search based on conditions you define for this context. For example, this could rely on the end users’s device, language, or the current category they’re visiting.