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 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 doesn’t have any 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 enablealternativesin 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 with an empty pattern,
Algolia applies the rule if users haven’t yet entered a query.
If a condition doesn’t include a 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 filtersincludescategory:TVruleContextsincludesmobile.
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. Examples of consequences include:- 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
aroundRadiusif your user’s query includes the words “near me”. - Remove word. Remove a specific word from your user’s (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
filtersoroptionalFiltersmatching 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.startsWith: the query starts with the condition string.endsWith: the query ends with the condition string.
is anchoring and set the condition’s pattern 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"} 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 one of the API clients.Using the dashboard
You can create a conditionless rule from the Algolia dashboard using either the Visual Editor or the Manual Editor.- Visual Editor
- Manual Editor
- In the Algolia dashboard, select your application and index, open Rules, and select New rule > Visual Editor. If you’re using the Merchandising Studio Visual Merchandiser instead, select Actions > Create new rule.
- In Conditions, select Create conditionless rule, review its scope, and click Apply. A conditionless rule applies to every search. Leaving a query field empty instead creates an empty-query condition.
- Select the consequences you want to apply. Optional: select Add a date range to limit when the rule applies.
- Click Review and Publish, review the rule, and click Publish.
Using the API
To run the code examples on this page, install the latest 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.These limitations only apply to context-only and conditionless rules.
Other rules can use the full range of consequences.
Choose a rule editor
Use the Visual Editor for most rules. It supports the same conditions and consequences as the Manual Editor and lets you preview their effect on search results. Choose another option when you need a different workflow.Rule tags
Create and manage rule tags in the Algolia dashboard or Merchandising Studio. On the Rules page, click Manage tags. A rule tag includes:
By default, indices may contain up to 100 rule tags.
After you create a rule tag, you can add it to individual rules in the Visual Editor and Manual Editor.