Rules Overview
On this page
Search needs to be flexible. When you hold a promotion, you shouldn’t need to reconfigure search settings or modify individual records. If one of your products isn’t appearing where you want it to in your search results, you should be able to reposition it quickly.
Rules let you make precise and (if desired) temporary modifications to your search results.
Relevance and index settings provide the general structure for your search results. Rules let you optimize and dynamically adjust that structure.
Rules don’t replace a good index configuration. Make sure you adjust your general relevance before adding Rules.
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.
There is a limit of 10K Rules per index.
Rules are typically used either for merchandising or fine-tuning your search and relevance.
To see some typical applications for Rules, check out the sections on merchandising with Rules and detecting user intent with Rules.
Structure of a Rule
Rules consist of three parts: conditions, consequences, and a validity period. Of these three parts, only the consequence is required. If one of a Rule’s conditions is met, the engine applies its consequences. If a Rule has no conditions, the engine always applies its consequences.
Rules target specific indices, but can be copied to replicas or other indices.
Conditions
A Rule can have up to 25 conditions. If a Rule has multiple conditions, it gets activated as soon as one of its conditions is met. In other words, conditions have an or
relationship to one another. Conditions can contain a pattern string, anchoring, a context, and/or filters, though none are required.
To decide if a Rule’s consequences should be applied, the engine:
- Compares the condition’s pattern string with a user’s query.
- 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 enable
alternatives
in the condition, the user’s query can match the condition’s pattern, even if the query is a plural, synonym, or typo of the pattern. If you don’t enablealternatives
, the user’s query must exactly match the condition’s pattern string 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
.
If a single condition includes a pattern string, filters, and a context, a search must match all three for the condition to be met. If you create three different conditions for each, only one must be met for the engine to activate the Rule.
If a condition has a pattern string that set to an empty string with is
anchoring, the engine applies the Rule whenever the user hasn’t yet entered a query.
If a condition has neither pattern, anchoring, context, or filters, it’s a conditionless Rule, and the engine applies it to every search.
These sections further describe the different types of conditions: query pattern, filters, context, and conditionless Rules.
Example conditions
Using a pattern with is
anchoring
1
2
3
4
{
"pattern": "sale",
"anchoring": "is",
}
The engine triggers a Rule with this condition whenever the query string is the word sale
and nothing else. If the query contains other words, the Rule isn’t triggered.
Using a pattern with contains
anchoring
1
2
3
4
{
"pattern": "featured",
"anchoring": "contains",
}
The engine triggers a Rule with this condition whenever the query string contains the word featured
. The query can include other words.
Using context
1
2
3
{
"context": "mobile"
}
The engine triggers a Rule with this condition whenever the search includes mobile
in the ruleContexts
.
Using filters
1
2
3
{
"filters": "category:TV"
}
The engine triggers a Rule with this condition whenever the filter { "filters": "category:TV" }
is applied to the query.
Using pattern, filters, and context
1
2
3
4
5
6
{
"pattern": "featured",
"anchoring": "contains",
"filters": "category:TV"
"context": "mobile"
}
The engine triggers a Rule with this condition whenever the query string contains the word featured
, the search includes category:TV
in its filters
, and includes mobile
in its ruleContexts
.
Using an empty string pattern
1
2
3
4
{
"pattern": "",
"anchoring": "is",
}
The engine triggers a Rule with this condition whenever there is an empty search. The Rule is turned off as soon as a user starts typing.
Consequences
Though conditions aren’t required, Rules must have at least one consequence. Consequences modify the results returned by a search. There are many possible consequences:
- Pin an Item: Inserts an item at a specific position. If the item is already in the list of results, this item is moved to that position.
- Hide an Item: Removes a specific result from the list of results.
- Add a Query Parameter: Adds 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: Removes a specific word from your user’s search query (for search, not display purposes).
- Replace Word: Replaces a word from your user’s search query with another word (for search, not display purposes).
- Replace Query: Replaces the user’s entire search query with another query (for search, not display purposes).
- Return Custom Data: Adds custom JSON data to the search response.
- Filter/Boost Matching Attributes: Applies
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. You can use this to automatically end promotions or sales on a specified end date.
Rules responding to user queries
Rules can parse your users’ queries and apply a consequence if the query matches the Rule’s condition. Queries are matched 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.
If you would like to trigger a Rule on an empty query or only use context, use is
anchoring, but set the text to be an empty string, or leave it blank in the dashboard.
Rules responding to applied filters
If a Rule’s condition includes filters, then the engine applies that Rule’s consequences only if the filters in a search’s query parameters exactly match the condition’s filters.
For example, the engine only triggers a Rule with the condition {"filters": "category:TV"}
, if category
is filtered on the valueTV
, and on this value only. 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 are triggered 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 would be triggered when a user lands on the TV category page.
For further considerations, check out the guide on implementing Rules triggered by filters.
To use an attribute in a Rule condition with filters, you must first declare it in attributesForFaceting
.
Rules responding to user context
If a Rule’s condition includes only a context, then that Rule’s consequence applies only if the Rule’s context exactly matches a value in the ruleContexts
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 must implement the logic to conditionally send contexts with your users’ queries. You can pass contexts through the ruleContexts
search parameter.
Implementation
To implement 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.
To clarify the process, consider this example. Suppose you own an electronics store. Using Analytics Tags, you’ve discovered that mobile users rarely find what they’re looking for on the first page of search results. With some testing, you realize that this is because vague searches for accessories like “chargers” or “cases” return laptop chargers and cases first.
You want to add a Rule that promotes all items in your catalog with the “phone” tag, but only if a user is searching from a mobile platform. Hence, if mobile users search for cases, you can assume that they prefer phone cases over laptop cases. If they search for chargers, phone chargers should show up first.
Conditionless Rules
Rules without conditions apply to every search on the index. They’re an effective way of temporarily modifying your site’s search for a predefined time, for example, for seasonal promotions.