Rules are no substitute for a well-configured index.
Make sure you adjust your general relevance before adding rules.
Index rules and composition rules
Algolia uses two different types of rules: index rules and composition rules. This section describes index rules. For more information about composition rules, see Smart Groups.What are rules used for?
Rules let you make precise, predetermined changes to your search results. For example, you can pin or hide items, boost or bury categories, or filter results based on the query. You can also enable rules for a fixed time, making rules a great way of implementing sales or promotions. Rules are typically used either for merchandising or fine-tuning your search and relevance. For more information about typical uses for rules, see Merchandising and Detecting intent.The maximum number of rules per index depends on your Algolia plan.
Rule structure
Three elements define a rule: conditions, consequences, and a validity period. Of these three elements, only a consequence is mandatory. When a rule’s condition is satisfied, Algolia applies its consequences. If a rule has no conditions (a conditionless rule), Algolia always applies its consequences. For more information, see:Conditions
A rule can have up to 25 conditions. A rule with several conditions activates when any condition is satisfied. In other words, conditions have anor
relationship to one another.
Conditions may contain a pattern string, anchoring, context, or filters.
To determine if a rule’s consequences apply:
-
Algolia compares the user’s query to the condition’s pattern.
It bases the comparison on the condition’s anchoring.
Values for anchoring are:
is
,contains
,starts with
, andends with
. The anchoring determines what part of the query the pattern must match: all of it, part of it, the beginning, or the end. If you enablealternatives
in the condition, user queries can match the condition’s pattern, even if the query is a plural, synonym, or typo of the pattern. Without alternatives, queries must precisely match the condition’s pattern and anchoring. - It compares the condition’s filters to the filters applied to the search.
-
It compares the condition’s context to the search’s
ruleContexts
.
is
anchoring, Algolia applies the rule if users haven’t yet entered a query.
If a condition has neither pattern, anchoring, context, or filters, it’s a conditionless rule, and Algolia applies it to every search.
Rules can respond to different types of conditions:
Example conditions
Using a pattern with is anchoring
Trigger a rule with this condition whenever the query string is the wordsale
and nothing else.
If the query contains other words, the rule isn’t triggered.
JSON
A pattern with contains anchoring
Trigger a rule with this condition whenever the query string contains the wordfeatured
.
The query can include other words.
JSON
A context
Trigger a rule when the search includesmobile
in the ruleContexts
.
JSON
Filters
Trigger a rule when the filter{ "filters": "category:TV" }
applies to the query.
JSON
The filters condition rejects an
OR
combination of different attributes.
For example, "filters": "brand:Guess OR color:orange"
returns an error.
You also can’t use numeric filters in conditions.Pattern, filters, and context
Trigger a rule when:- The query contains the word
featured
filters
includescategory:TV
ruleContexts
includesmobile
.
JSON
An empty string pattern
Trigger a rule when there is an empty search. The rule turns off as soon as a user starts typing.JSON
Consequences
Though conditions aren’t required, rules must have at least one consequence. Consequences change the search results. Possible consequences are:- Pin an Item. Insert an item at a specific position. If the item is already in the results, it moves to that position.
- Hide an Item. Remove a specific result from the list of results.
- Add a Query Parameter. Add a query parameter to your user’s search. For example, you could decrease the
aroundRadius
if your user’s query includes the words “near me”. - Remove Word. Remove a specific word from your user’s search query (for search, not display purposes).
- Replace Word. Replace a word from your user’s search query with another word (for search, not display purposes).
- Replace Query. Replace the entire user search query with another query (for search, not display purposes).
- Return Custom Data. Add custom JSON data to the search response.
- Filter/Boost Matching Attributes. Apply
filters
oroptionalFilters
matching the query to the results.
Validity period
If you want to apply a rule temporarily, you can set a validity period. The validity period determines how long a rule remains active. Use this to ensure promotions and sales end automatically.Rules responding to user queries
Rules can parse user queries and apply consequences if the query matches the rule’s condition. Queries can match with conditions in the following ways:- is: the entire query matches the condition string.
- contains: the entire query contains the condition string
- starts with: the query starts with the condition string.
- ends with: the query ends with the condition string.
is
anchoring but set the text to an empty string.
Rules responding to applied filters
If a rule’s condition includes filters, Algolia applies that rule’s consequences only if the filters in a search’s query parameters exactly match the condition’s filters. For example, Algolia only triggers a rule with the condition{"filters": "category:TV"}
, only if the category
value is TV
.
Other filters like "category:Smartphone"
or "category:TV OR category:Smartphone"
don’t trigger the rule.
The primary goal of this condition is to create rules that trigger on specific category pages or when a user applies specific filters.
For example, you can define a rule with the condition {"filters": "category:TV"}
and a consequence to promote specific items.
The rule triggers when a user lands on the TV category page.
For more information, see Filters can trigger rules.
To use an attribute in a rule condition with filters,
you must first declare it in
attributesForFaceting
.Context-only rules
If a rule’s condition only includes a context, the consequence only applies if the rule’s context exactly matches a value in theruleContexts
parameter of a user’s search.
What are contexts?
Contexts provide information about your user’s search environment: for example, what section of a website they’re currently visiting or what device they’re using. A context can be any string value that doesn’t include whitespace characters. The search condition it specifies must be identifiable at search time. You need to implement logic for sending contexts with queries. You can pass contexts through theruleContexts
search parameter.
Implementation
To create a rule with a context condition, you must:- Create the rule (with the dashboard or the API).
- Conditionally assign its associated context to your user’s searches.
Conditionless rules
Rules without conditions apply to every search. They’re an effective way of temporarily modifying search for a predefined time, for example, for seasonal promotions. For more information, see Add default search parameters with rules. You can create conditionless rules from the Algolia dashboard or with an API client.Create a conditionless rule from the dashboard
- Select the Search product icon in the Algolia dashboard.
- Select the Rules section from the left sidebar menu in the dashboard.
- Under the heading Rules, select the index you’re adding a rule to.
- Select Create your first rule or New rule. In the drop-down menu, click Manual Editor. You can’t create conditionless rules with the Visual Editor.
- In the Condition(s) section, click the Remove button with the trash can icon at the top right of the condition.
- In the Consequence(s) section, set the relevant consequences.
- If this rule only applies for a certain period, select a time range in Additional Settings, under Timeframe in UTC.
- If you want to forward the rule to replicas or other indices, toggle Copy this rule to other indices and enter the relevant names.
- Save your changes.
Create a conditionless rule with an API client
To create a conditionless rule with an API client, use thesaveRule
method on a rule object but without a condition.
Context-only and conditionless rules deactivate consequences
Since conditionless and context-only rules don’t have a query as a condition, these consequences are deactivated.Consequence | Why is it deactivated? |
---|---|
Pin an item | The same item would always appear in the same position, usually at the top, for all queries and all users. Instead, consider adding a banner to promote items you want to pin. |
Remove Word, Replace Word, Replace Query. | Since there’s no query, there are no words to remove or replace. |
Filter/Boost Matching Attributes | Rules that filter and boost facets require users to type a facet attribute into the query. Since there’s no query or facet in the condition, there’s no filter for the rule to boost |
These limitations only apply to context-only and conditionless rules.
Other rules can use the full range of consequences.
Visual Editor or Manual Editor
You can use the Visual Editor or Manual Editor to create rules.- The Visual Editor has the most visual way of configuring rules but doesn’t offer all capabilities.
- The Manual Editor has more capabilities but is less visual.
Option | Capabilities |
---|---|
Visual Editor | • Promote and hide items • Boost, bury, and filter categories • Set priority on category ordering • Ban categories of items • Merchandise on filter selection • Merchandise based on date range for time-limited promotions • Merchandise based on geolocation, device, and user segment • Facet merchandising (facet type and value reordering) • Display banners • Configurable preview UI |
Manual Editor | • Merchandise on filter selection • Add custom data to rules • Display advanced banners |
Rules API | • Explore the Rules API • Build a custom merchandising UI |