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:

  1. 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.
  2. 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.
  3. Fetch rule products. Custom items come first, in the order listed. Custom query results follow, in rule order.
  4. 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.
  5. 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 fromPublic filters from the requestRecommender filters and filter rulesRule's own query filters
Custom itemsNoNo–
Custom query rulesYesNoYes
Recommender sourcesYesYes–

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 specificId condition or a custom items action.
  • Up to 10 values in a termFilter condition.

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 }
    }
  }
}
FieldDescription
conditionsWhen the rule applies. See Conditions.
conditionMatchall: every condition must match. any: at least one condition must match.
behaviormerge or replace. See How rules are executed.
actionWhat the rule does. See Actions.

Conditions

TypeMatches whenOn _all requests
alwaysEvery request.Matches
specificIdThe requested product id is in the list.Never matches
attributeMatchThe requested product has one of the given attribute values.Never matches
attributeDoesNotMatchThe requested product does not have any of the given attribute values.Always matches
queryParamThe request URL has the given query parameter, with any non-empty value.Works as usual
termFilterThe request has a public filter on the given attribute with one of the values.Works as usual
dateThe 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

TypeWhat it does
filterFilters, excludes or boosts products returned by the recommender sources.
searchQueryRuns a custom query and puts its results before the source results.
customIdsPuts a fixed list of products first, in the given order.
sourceWeightOverrideChanges 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 } }
}
FieldDescription
filtersOnly keep products that match. Uses the same format as search query filters.
exclusionFiltersRemove products that match.
boostBoost 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
  }
}
FieldDescription
searchTextText to search for. Leave it empty to match all products, and use filters and boosts to choose and rank them.
queryFieldsFields to search, with weights. Defaults to the fields of the recommender's search query.
filtersOnly return matching products. Public filters from the request are added to these.
exclusionFiltersNever return matching products.
boostBoost by a numeric field ("rating": 2) or by attribute values ("brand": { "Apple": 5 }).
statisticalBoostBoost popular products by clicks and add-to-cart events, e.g. { "itemClick": 1.5, "addToCart": 2.5 }.
personalizationBoost products similar to what this shopper interacted with. See Personalized recommendations.
sortSort order. _relevance sorts by score (including boosts and personalization), _clickCount by click count. Any sortable field also works.
collapseReturn 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.
forceItemOrderReturn only these product ids, in this order. Unlike custom items, filters still apply. Leave sort empty when you use it.
limitHow 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 limit below the recommender limit. If rule products fill the whole limit, the sources don't run at all. A rule limit of about half the recommender limit keeps room for the sources.
  • Collapse only by single-value fields. Each product must have one value in the collapse field, 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 filters too.
  • With merge, several matching custom query rules all add their results, in rule order. With replace, 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 forceItemOrder instead.

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:

ValueReplaced withExample
[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, level cuts each value to that depth, counting from 0. { "level": 0, "terms": ["[category]"] } turns Electronics > Phones > Cases into Electronics.
  • 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 _all requests, which have no product.
  • Product values come from the products the conditions matched. always, date, specificId, attributeMatch and attributeDoesNotMatch provide the requested product. queryParam and termFilter don't provide any. To use [field] values in a rule with only these conditions, add an always condition and set conditionMatch to all.

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.

ConditionMatches when[field] values come from
specificIdAny requested product is listedAll requested products
attributeMatchAny requested product matchesOnly the requested products that match
attributeDoesNotMatchNone of the requested products matchAll requested products
always, dateAs usualAll requested products
queryParam, termFilterAs usualNone
  • 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 rule limit. To target one kind of product, use attributeMatch. 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 the itemId parameters 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]"] } }
  }
}

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"] }
  }
}

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
      }
    }
  }
}
{
  "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.