Recommendation rules
Recommendation rules let you change what a recommender returns in specific situations, without creating a new recommender. A rule has:
- Conditions: when the rule applies. For example: the viewed product is a phone, the request comes from the cart page, or it is Black Friday.
- An action: what the rule does. For example: show phone cases first, add a fixed list of products, or show only products that are in stock.
Rules belong to one recommender. They work on top of its sources (similar items, trending items, recent activity, and so on).
How rules are executed
For every recommendation request:
- Check the conditions. Each enabled rule is checked against the request: the requested product, query parameters, public filters and the current date. Rules that are disabled are skipped.
- Apply the actions. Every matching rule is applied, not only the first one. Rules are applied in their order in the list, from top to bottom. Each rule's behavior decides what happens when an earlier rule did the same kind of action:
merge: add to what earlier rules did. Custom items and custom query results are added after the earlier ones, and filters are combined.replace: discard what earlier rules of the same action type did, and use only this rule's action.
- Fetch rule products. Custom items come first, in the order listed. Custom query results follow, in rule order.
- Fetch source products. The recommender sources run with any filters and source overrides from rules. If rule products already fill the recommender
limit, the sources are skipped. - Combine. Rule products come first, then source products. Duplicates are removed, and the list is cut to
limit.
request ─► check conditions ─► apply matching rules (top to bottom)
│
┌────────────────────────┴───────────────────────┐
▼ ▼
rule products source products
1. custom items (similar, trending, recent activity…)
2. custom query results + recommender filters
+ public filters
+ filter rules
└────────────────────────┬───────────────────────┘
▼
rule products first ─► remove duplicates ─► cut to limit
Which filters apply where
Filters apply differently to each kind of product. This is the most common source of surprises:
| Products from | Public filters from the request | Recommender filters and filter rules | Rule's own query filters |
|---|---|---|---|
| Custom items | No | No | – |
| Custom query rules | Yes | No | Yes |
| Recommender sources | Yes | Yes | – |
If a restriction must hold for every recommended product (for example "in stock only"), add it in both places: as a filter rule, which covers sources, and in the filters of each custom query rule. Custom items are always returned as listed, so only add products that you are sure about.
Rule limits
- Up to 10 conditions per rule.
- Up to 50 product ids in a
specificIdcondition or a custom items action. - Up to 10 values in a
termFiltercondition.
Rule structure
{
"name": "Phone accessories",
"enabled": true,
"configuration": {
"conditions": [{ "type": "attributeMatch", "match": { "category": ["Phones"] } }],
"conditionMatch": "all",
"behavior": "merge",
"action": {
"type": "searchQuery",
"query": { "filters": { "category": ["Phone cases", "Chargers"] }, "limit": 4 }
}
}
}
| Field | Description |
|---|---|
conditions | When the rule applies. See Conditions. |
conditionMatch | all: every condition must match. any: at least one condition must match. |
behavior | merge or replace. See How rules are executed. |
action | What the rule does. See Actions. |
Conditions
| Type | Matches when | On _all requests |
|---|---|---|
always | Every request. | Matches |
specificId | The requested product id is in the list. | Never matches |
attributeMatch | The requested product has one of the given attribute values. | Never matches |
attributeDoesNotMatch | The requested product does not have any of the given attribute values. | Always matches |
queryParam | The request URL has the given query parameter, with any non-empty value. | Works as usual |
termFilter | The request has a public filter on the given attribute with one of the values. | Works as usual |
date | The current date matches a day, a date range or a weekday. | Works as usual |
_all requests (/recommendations/document/_all) have no viewed product, for example on a home page. Conditions about the product can't match them. Use always, queryParam, termFilter and date for those placements.
always
Matches every request. Use it for rules that should always run, such as global filters or a personalized custom query.
{ "type": "always" }
specificId
Matches when the requested product id is one of the listed ids. When several ids are requested at once (for example the products in a cart), it matches if any of them is listed.
{ "type": "specificId", "ids": ["PRN-1020", "PRN-1021"] }
attributeMatch
Matches when the requested product has at least one of the listed values for every listed attribute. Values are compared exactly, and case matters.
{ "type": "attributeMatch", "match": { "category": ["Phones"], "brand": ["Apple", "Samsung"] } }
You can also check that an attribute is present and not empty:
{ "type": "attributeMatch", "match": { "warranty_options": { "exists": true } } }
Range and hierarchy filters are not supported in this condition. A rule that uses them never matches.
attributeDoesNotMatch
The opposite of attributeMatch. It matches when the requested product does not match the given values.
{ "type": "attributeDoesNotMatch", "match": { "on_sale": ["true"] } }
queryParam
Matches when the request URL contains the parameter. Any parameter works, so you can use your own parameters to tell placements apart:
GET /v1/recommendations/document/_all?recommendationQueryKey=r-xxxxxxxxxxxx&placement=cart
{ "type": "queryParam", "paramName": "placement" }
The condition only checks that the parameter is present. To act on its value, use a [q:placement] special value in the action.
termFilter
Matches when the request has a public filter on key with at least one of the values. Works with both term filters and hierarchy filters.
GET /v1/recommendations/document/_all?recommendationQueryKey=r-xxxxxxxxxxxx&filters[category][]=Running shoes
{ "type": "termFilter", "key": "category", "values": ["Running shoes", "Trail shoes"] }
date
Dates are checked in UTC.
{ "type": "date", "match": "dateRange", "dateFrom": "2026-11-27T00:00:00Z", "dateTo": "2026-11-30T23:59:59Z" }
{ "type": "date", "match": "exactDay", "date": "2026-12-24" }
{ "type": "date", "match": "weekday", "weekday": 6 }
weekday is 1 for Monday through 7 for Sunday.
Actions
| Type | What it does |
|---|---|
filter | Filters, excludes or boosts products returned by the recommender sources. |
searchQuery | Runs a custom query and puts its results before the source results. |
customIds | Puts a fixed list of products first, in the given order. |
sourceWeightOverride | Changes the recommender's source configuration for matching requests. |
Filter
Narrows down products from the recommender sources.
{
"type": "filter",
"filters": { "stock_status": ["IN_STOCK"] },
"exclusionFilters": { "tags": ["Discontinued"] },
"boost": { "brand": { "[brand]": 3 } }
}
| Field | Description |
|---|---|
filters | Only keep products that match. Uses the same format as search query filters. |
exclusionFilters | Remove products that match. |
boost | Boost matching products. Only affects the similar items (moreLikeThis) source. |
With merge, the filters of all matching filter rules are combined. With replace, only this rule's filters are used.
Filter rules do not affect custom items or custom query results. See Which filters apply where.
Custom query
Runs a separate product query. Its results are placed before the source results. This is the most flexible action: you can choose any products with filters, rank them with boosts and sorting, and personalize them.
{
"type": "searchQuery",
"query": {
"searchText": "",
"filters": { "category": ["Phone cases"] },
"exclusionFilters": { "id": ["[id]"] },
"boost": { "rating": 2 },
"sort": [{ "_relevance": "desc" }],
"limit": 4
}
}
| Field | Description |
|---|---|
searchText | Text to search for. Leave it empty to match all products, and use filters and boosts to choose and rank them. |
queryFields | Fields to search, with weights. Defaults to the fields of the recommender's search query. |
filters | Only return matching products. Public filters from the request are added to these. |
exclusionFilters | Never return matching products. |
boost | Boost by a numeric field ("rating": 2) or by attribute values ("brand": { "Apple": 5 }). |
statisticalBoost | Boost popular products by clicks and add-to-cart events, e.g. { "itemClick": 1.5, "addToCart": 2.5 }. |
personalization | Boost products similar to what this shopper interacted with. See Personalized recommendations. |
sort | Sort order. _relevance sorts by score (including boosts and personalization), _clickCount by click count. Any sortable field also works. |
collapse | Return only the top product per value of a field, e.g. { "field": "product_group_id" } for one variant per product. The field must have a single value per product. See the tips below. |
forceItemOrder | Return only these product ids, in this order. Unlike custom items, filters still apply. Leave sort empty when you use it. |
limit | How many products this rule adds. Defaults to 10. |
Tips:
- Exclude the viewed product. A custom query does not exclude the product the recommendations are for. Add
"exclusionFilters": { "id": ["[id]"] }on product pages. - Keep
limitbelow the recommenderlimit. If rule products fill the whole limit, the sources don't run at all. A rulelimitof about half the recommenderlimitkeeps room for the sources. - Collapse only by single-value fields. Each product must have one value in the
collapsefield, e.g."brand": "Nike". If any product in the results has a list of values, e.g."color": ["Red", "Blue"], the whole recommendation request fails, not just this rule. A field that has a single value for most products can still fail when the wrong product appears in the results, so choose a field that is single-valued for every product, such as a product group id. - Put shared restrictions in both places. Recommender filters and filter rules don't apply here. Add the same restriction (e.g. stock status) to the custom query's
filterstoo. - With
merge, several matching custom query rules all add their results, in rule order. Withreplace, only the results of the last matching custom query rule are used.
Custom items
Adds a fixed list of products before all other results, in the given order.
{ "type": "customIds", "ids": ["INK-301", "INK-302", "PAPER-A4"] }
- The product the recommendations are for is removed from the list automatically.
- No filters apply: not the public filters, the recommender filters or filter rules. Products are returned even if they are out of stock, as long as they exist in the index.
- If you need filters to apply, use a custom query with
forceItemOrderinstead.
With merge, ids from several matching rules are combined in rule order, without duplicates. With replace, only this rule's ids are used.
Source weight override
Replaces the configuration of the recommender's sources for matching requests. Each listed source replaces the recommender's source of the same type. Source types the recommender doesn't have are added.
{
"type": "sourceWeightOverride",
"sources": [{ "type": "trendingItems", "weight": 40, "maxAgeDays": 3 }]
}
Up to 5 sources per rule.
Special values: dynamic filters and boosts
Inside filters, exclusionFilters, boost and forceItemOrder, a value in square brackets is replaced when the request is made:
| Value | Replaced with | Example |
|---|---|---|
[field] | The value(s) of field on the requested product | "brand": ["[brand]"] |
[a.b] | A nested field on the requested product | "vendor": ["[seller.name]"] |
[q:param] | The value(s) of the param query parameter of the request | "category": ["[q:category]"] |
- If the product has several values (e.g. tags), all of them are used, up to 50.
- In a hierarchy filter,
levelcuts each value to that depth, counting from 0.{ "level": 0, "terms": ["[category]"] }turnsElectronics > Phones > CasesintoElectronics. - If nothing is found, the filter is dropped, not left empty. For example,
"brand": ["[brand]"]on a product without a brand shows all brands. The same happens on_allrequests, which have no product. - Product values come from the products the conditions matched.
always,date,specificId,attributeMatchandattributeDoesNotMatchprovide the requested product.queryParamandtermFilterdon't provide any. To use[field]values in a rule with only these conditions, add analwayscondition and setconditionMatchtoall.
Requests with several products
You can request recommendations for up to 20 products at once, for example for the products in a cart:
GET /v1/recommendations/documentsByMultipleIds?recommendationQueryKey=r-xxxxxxxxxxxx&itemId=PHONE-1&itemId=CAM-7&itemId=BOOK-3
Rules are checked once for the whole request, not once per product.
| Condition | Matches when | [field] values come from |
|---|---|---|
specificId | Any requested product is listed | All requested products |
attributeMatch | Any requested product matches | Only the requested products that match |
attributeDoesNotMatch | None of the requested products match | All requested products |
always, date | As usual | All requested products |
queryParam, termFilter | As usual | None |
- Values from several products are combined. With
"category": ["[category]"]and a cart with a phone, a camera and a book, the custom query looks for phones, cameras and books together. Results are not balanced per product, so one category can fill the whole rulelimit. To target one kind of product, useattributeMatch. Its[field]values only come from the matching products. - Custom items skip every requested product.
- Custom queries can return products that are already in the request.
"exclusionFilters": { "id": ["[id]"] }only excludes the products the condition matched (see the table). To exclude every requested product, use"exclusionFilters": { "id": ["[q:itemId]"] }. It reads theitemIdparameters of the request, so it only works with this endpoint.
Example: when the cart contains a phone, show cases that fit the phone's brand, and skip everything already in the cart.
{
"name": "Cases for the phone in cart",
"configuration": {
"conditions": [{ "type": "attributeMatch", "match": { "category": ["Phones"] } }],
"conditionMatch": "all",
"behavior": "merge",
"action": {
"type": "searchQuery",
"query": {
"filters": { "category": ["Phone cases"], "compatible_brand": ["[brand]"], "stock_status": ["IN_STOCK"] },
"exclusionFilters": { "id": ["[q:itemId]"] },
"limit": 3
}
}
}
}
Only the phone's brand is used, even if the cart also has products of other brands.
Examples
In stock only, everywhere
One rule for sources, plus the same filter in each custom query.
{
"name": "Only in-stock products",
"configuration": {
"conditions": [{ "type": "always" }],
"conditionMatch": "all",
"behavior": "merge",
"action": { "type": "filter", "filters": { "stock_status": ["IN_STOCK"] } }
}
}
Accessories for the viewed phone, same brand first
On a phone page, show cases and chargers, preferring the phone's own brand.
{
"name": "Phone accessories",
"configuration": {
"conditions": [{ "type": "attributeMatch", "match": { "category": ["Phones"] } }],
"conditionMatch": "all",
"behavior": "merge",
"action": {
"type": "searchQuery",
"query": {
"filters": { "category": ["Phone cases", "Chargers"], "stock_status": ["IN_STOCK"] },
"boost": { "compatible_brand": { "[brand]": 10 }, "rating": 1 },
"limit": 4
}
}
}
}
"More from this brand" recommender
A recommender for a "More from this brand" placement. A filter rule limits the sources to the viewed product's brand.
{
"name": "Same brand only",
"configuration": {
"conditions": [{ "type": "always" }],
"conditionMatch": "all",
"behavior": "merge",
"action": { "type": "filter", "filters": { "brand": ["[brand]"] } }
}
}
Related products from your product feed
If your feed lists related products per product (e.g. related_ids), show them first, in the feed's order, skipping any that are out of stock.
{
"name": "Feed-defined related products",
"configuration": {
"conditions": [{ "type": "attributeMatch", "match": { "related_ids": { "exists": true } } }],
"conditionMatch": "all",
"behavior": "merge",
"action": {
"type": "searchQuery",
"query": {
"forceItemOrder": ["[related_ids]"],
"filters": { "stock_status": ["IN_STOCK"] },
"limit": 6
}
}
}
}
Stay in the same top-level category
On a product in Home > Kitchen > Knives, recommend only products under Home.
{
"type": "filter",
"filters": { "category_hierarchy": { "level": 0, "terms": ["[category_hierarchy]"] } }
}
Ink cartridges for specific printers
{
"name": "Printer consumables",
"configuration": {
"conditions": [{ "type": "specificId", "ids": ["PRN-1020", "PRN-1021"] }],
"conditionMatch": "all",
"behavior": "merge",
"action": { "type": "customIds", "ids": ["INK-301", "INK-302", "PAPER-A4"] }
}
}
Black Friday featured products
During the campaign, pin the promoted products first on every placement.
{
"name": "Black Friday featured",
"configuration": {
"conditions": [
{ "type": "date", "match": "dateRange", "dateFrom": "2026-11-27T00:00:00Z", "dateTo": "2026-11-30T23:59:59Z" }
],
"conditionMatch": "all",
"behavior": "merge",
"action": { "type": "customIds", "ids": ["TV-55-OLED", "HEADPHONES-X2"] }
}
}
Cheap add-ons on the cart page
Call the recommender with an extra &cartPage=1 parameter from the cart page, and add low-priced, popular add-ons there.
{
"name": "Cart add-ons",
"configuration": {
"conditions": [{ "type": "queryParam", "paramName": "cartPage" }],
"conditionMatch": "all",
"behavior": "merge",
"action": {
"type": "searchQuery",
"query": {
"filters": { "price": { "lte": 15 }, "stock_status": ["IN_STOCK"] },
"sort": [{ "_clickCount": "desc" }],
"limit": 3
}
}
}
}
New collection first on a category page
A category page calls the recommender with the public filter filters[category][]=Running shoes. Pin the new collection first on that page only.
{
"name": "New running collection",
"configuration": {
"conditions": [{ "type": "termFilter", "key": "category", "values": ["Running shoes"] }],
"conditionMatch": "all",
"behavior": "merge",
"action": { "type": "customIds", "ids": ["RUN-2027-01", "RUN-2027-02"] }
}
}
Custom items ignore public filters, so make sure the pinned products belong to the category. Custom queries, on the other hand, always get the public filters added. A custom query with "category": ["Running socks"] on this page would look for products that are both socks and running shoes, and return nothing.
Discounted alternatives for full-price products
On a product that is not on sale, show discounted products from the same category.
{
"name": "Cheaper alternatives",
"configuration": {
"conditions": [{ "type": "attributeDoesNotMatch", "match": { "on_sale": ["true"] } }],
"conditionMatch": "all",
"behavior": "merge",
"action": {
"type": "searchQuery",
"query": {
"filters": { "category": ["[category]"], "on_sale": ["true"] },
"exclusionFilters": { "id": ["[id]"] },
"sort": [{ "discount": "desc" }],
"limit": 3
}
}
}
}
More trending products at weekends
{
"name": "Weekend trending",
"configuration": {
"conditions": [
{ "type": "date", "match": "weekday", "weekday": 6 },
{ "type": "date", "match": "weekday", "weekday": 7 }
],
"conditionMatch": "any",
"behavior": "merge",
"action": {
"type": "sourceWeightOverride",
"sources": [{ "type": "trendingItems", "weight": 40, "maxAgeDays": 2 }]
}
}
}
Testing rules
The recommender test in the LupaSearch console runs the recommender with your unsaved rules and shows which rules were applied to each product. Test with:
- A product that should match the rule, and one that shouldn't.
_all, if the recommender is also used without a product.- Public filters and query parameters, if your rules depend on them.