- Sort all matching rules with a tie-breaking algorithm.
- Compare the sorted rules to check for conflicts. If there is a conflict, exclude rules with the lowest precedence.
- Apply the remaining rules to the query.
Rules precedence
Algolia uses a tie-breaking algorithm to determine the precedence of all rules that apply to a query. The next criterion is only considered if there’s a tie in the preceding criteria. More specific rules typically have higher precedence. The tie-breaking algorithm that determines rule precedence applies these criteria:- Position. The match closest to the beginning of the query string wins.
- Match length. The longest match wins. The match length is determined by the number of words matched from the query string and the number of filters matched from the query parameters.
- Anchoring. Rules with anchoring patterns are ranked in this order:
is
>starts with
>ends with
>contains
. - Context. Rules with context have a higher priority than other rules.
- Filters. Rules with filters have a higher priority than other rules.
- Literals over placeholders. If a word matches a literal and a facet value, the literal takes precedence. Matches with alternatives, such as synonyms and plurals, don’t take precedence over matches with facet values.
- Temporary over permanent. If the temporary and permanent rules match, the temporary rule takes precedence.
- Rule ID. If there are still conflicts after all other criteria have been applied, the rule with the smallest rule ID (
objectID
) wins, in lexicographical order wins.
For more information about rules applied to recommendations,
see Recommend rules.
Conflicting conditions
After ranking rules by precedence, only conflicting rules with the highest precedence are retained. Conflicting rules with lower precedence are excluded.Overlapping pattern conditions
Rules with the pattern condition might conflict with each other due to overlapping patterns. Rules with overlapping patterns are excluded. Consider the queryEnchanted forest adventure
, and an index with several rules.
Each rule matches a different pattern.
The rules are ranked by precedence and only rules A and D are retained.
The pattern of rules B and C overlaps with rule A, which leads to their exclusion.
Text
Rules with an empty query pattern don’t overlap with any other pattern.
They will all be applied, as long as there isn’t any other conflict.
Intersecting filter conditions
For rules with intersectingfilters
conditions, the rule with the lowest precedence is excluded.
Conflicting consequences
Two rules might try to apply the same consequences, for example, pinning different records to the first position. Consider the following example:- Rule 1: if a query contains “Shakespeare”, then promote the record “The Lost Shakespeare Diaries”, with objectID “1”, to the first result.
- Rule 2: if a query contains the phrase “how much is”, then promote the record “Full Price List”, with objectID “2”, to the first result.
objectID
of the promoted record.
The record with the lowest objectID
(in lexicographical order) wins.
Subsequent promotions are shifted down by one position.
In the previous example, the record “The Lost Shakespeare Diaries” is promoted to the first result, since it has a smaller objectID
.
The record “Full Price List” is then pinned to the second position.
Edge cases
If a rule removes a word from the query string, all subsequent rules that would have been triggered by this word are ignored. If a rule replaces the query string entirely, all subsequent rules are ignored.Context-only rules
For rules that have only a context as a condition, the following applies:- They have a higher priority than general rules and rules without conditions.
- They always trigger in this context and never conflict with other rules.
Rules without conditions
For rules that don’t have any conditions, the following applies:- They have a higher priority than general rules.
- They always trigger and never conflict with other rules.
Effect of promotions on relevance
Only records from the same index can be promoted. Promoted records have to be explicitly identified by theirobjectID
.
A promoted record is included in search results, even if it doesn’t match the query.
If it matches the query, it’s removed from its original position and inserted at its promoted position,
even if the original position was better than the promoted position.
Promoted positions are restricted to a range of 0 to 300.
Inside the same rule, each promoted record must have a unique promoted position.
If promoted records from two distinct rules are triggered for the same query,
the following applies:
- Duplicates are merged, using the best position.
- If the resulting positions conflict between distinct records, records are shifted down until a free slot is found.
- All regular search results are shifted down to ensure that all promoted records are as close to their promoted position as possible.