Recommendation patterns
Write the two feature shapes that recommendation and ranking models ask for most: a feature of a (user, item) pair, and a user's value compared with the average of a group. Both have a direct form that FeatureQL refuses, and a form that works in batch and in serving.
Before you start, know how RELATED() aggregates a child source onto a parent key ( RELATED ) and how CARRY brings an outer value into a TRANSFORM ( TRANSFORM ).
Features of a (user, item) pair
A ranking model scores candidate items for a user. Its strongest features describe the pair: how often this user viewed this item, whether they already bought it.
The direct form, a RELATED(COUNT() ON …) matching both the user and the item, is refused. RELATED ... ON joins two grains, the key you compute for and the rows you aggregate. A pair of keys plus the events is three:
DEFINE
USERS := ENTITY(),
ITEMS := ENTITY(),
EVENTS := ENTITY(),
USER_ID := INPUT(BIGINT#USERS),
ITEM_ID := INPUT(BIGINT#ITEMS),
EVENT_ID := INPUT(BIGINT#EVENTS),
EV := INLINE_COLUMNS(
event_id BIGINT#EVENTS BIND TO EVENT_ID,
user_id BIGINT#USERS,
item_id BIGINT#ITEMS
FROM JSON([{"event_id": 1, "user_id": 1, "item_id": 10}])
),
PAIR_EVENTS := RELATED(COUNT() ON EV[user_id] = USER_ID AND EV[item_id] = ITEM_ID),
RETURN USER_ID, ITEM_ID, PAIR_EVENTS
FOR CROSS
USER_ID := BIND_VALUES(ARRAY(1)),
ITEM_ID := BIND_VALUES(ARRAY(10)),
NESTED EVENT_ID := BIND_VALUES(ARRAY(1));Collect the user's history once, at the user's grain, as an array of rows. Then, for each candidate item, CARRY the item into the array and keep the matching rows:
DEFINE
users := ENTITY(),
items := ENTITY(),
events := ENTITY(),
user_id := INPUT(BIGINT#users),
item_id := INPUT(BIGINT#items),
event_id := INPUT(BIGINT#events),
ev := INLINE_COLUMNS(
event_id BIGINT#events BIND TO event_id,
user_id BIGINT#users,
item_id BIGINT#items,
kind VARCHAR
FROM JSON([
{"event_id": 1, "user_id": 1, "item_id": 10, "kind": "view"},
{"event_id": 2, "user_id": 1, "item_id": 10, "kind": "view"},
{"event_id": 3, "user_id": 1, "item_id": 10, "kind": "buy"},
{"event_id": 4, "user_id": 1, "item_id": 20, "kind": "view"},
{"event_id": 5, "user_id": 2, "item_id": 10, "kind": "view"}
])
),
user_history := user_id.RELATED(
ARRAY_AGG(ROW(ev[item_id] AS item_id, ev[kind] AS kind))
GROUP BY (ev[user_id])
),
pair_views := ARRAY_COUNT(
user_history.CARRY(item_id AS candidate).TRANSFORM(
RETURN kind
WHERE item_id = candidate AND kind = 'view'
).UNWRAP()
),
pair_bought := ARRAY_COUNT(
user_history.CARRY(item_id AS candidate).TRANSFORM(
RETURN kind
WHERE item_id = candidate AND kind = 'buy'
).UNWRAP()
)
> 0
RETURN
user_id,
item_id,
pair_views,
pair_bought
FOR CROSS
user_id := BIND_VALUES(ARRAY(1, 2)),
item_id := BIND_VALUES(ARRAY(10, 20, 30)),
NESTED event_id := BIND_VALUES(ARRAY(1, 2, 3, 4, 5))
ORDER BY
user_id,
item_id
;| USER_ID BIGINT#USERS | ITEM_ID BIGINT#ITEMS | PAIR_VIEWS BIGINT | PAIR_BOUGHT BOOLEAN |
|---|---|---|---|
| 1 | 10 | 2 | true |
| 1 | 20 | 1 | false |
| 1 | 30 | 0 | false |
| 2 | 10 | 1 | false |
| 2 | 20 | 0 | false |
| 2 | 30 | 0 | false |
User 1 viewed item 10 twice and bought it. Item 30 is absent from the history, so its counts are 0. USER_HISTORY is computed once per user, whatever the number of candidates, and each pair feature is a filter over that array.
Cost. The array holds every event of the user that the history keeps. Bound it in the aggregate with a FILTER (WHERE …) on recent events, or keep only the fields you filter on. A heavy user's history is the size to measure before serving ( Choosing a pattern ).
A user's value against their group's average
"My spend compared with my country's average" is AVG(spend) OVER (PARTITION BY country) in SQL. In FeatureQL the direct form averages a feature that is itself a RELATED aggregate at the same grain, and it is refused:
DEFINE
CUSTOMERS := ENTITY(),
ORDERS := ENTITY(),
CUSTOMER_ID := INPUT(BIGINT#CUSTOMERS),
ORDER_ID := INPUT(BIGINT#ORDERS),
CUST := INLINE_COLUMNS(
customer_id BIGINT#CUSTOMERS BIND TO CUSTOMER_ID,
country VARCHAR
FROM JSON([{"customer_id": 1, "country": "FR"}])
),
ORD := INLINE_COLUMNS(
order_id BIGINT#ORDERS BIND TO ORDER_ID,
customer_id BIGINT#CUSTOMERS,
amount BIGINT
FROM JSON([{"order_id": 10, "customer_id": 1, "amount": 30}])
),
SPEND := CUSTOMER_ID.RELATED(SUM(ORD[amount]) GROUP BY (ORD[customer_id])),
COUNTRY_AVG := CUSTOMER_ID.RELATED(AVG(SPEND) GROUP BY (CUST[country])),
RETURN CUSTOMER_ID, COUNTRY_AVG
FOR CUSTOMER_ID := BIND_VALUES(ARRAY(1)), NESTED ORDER_ID := BIND_VALUES(ARRAY(10));Compute the group's total and its size as two separate aggregates, each matched on the country:
- The group's total. Give each order its customer's country (
RELATED(CUST[country] VIA ORD[customer_id])), then sum the orders whose country is the customer's. - The group's size. Declare a second entity,
PEERS, over the same customers table, and count the peers whose country is the customer's. A second entity lets the customers table be both the key you compute for and the rows you count.
DEFINE
customers := ENTITY(),
peers := ENTITY(),
orders := ENTITY(),
customer_id := INPUT(BIGINT#customers),
peer_id := INPUT(BIGINT#peers),
order_id := INPUT(BIGINT#orders),
cust := INLINE_COLUMNS(
customer_id BIGINT#customers BIND TO customer_id,
country VARCHAR
FROM JSON([
{"customer_id": 1, "country": "FR"},
{"customer_id": 2, "country": "FR"},
{"customer_id": 3, "country": "DE"}
])
),
peer := INLINE_COLUMNS(
customer_id BIGINT#peers BIND TO peer_id,
country VARCHAR
FROM JSON([
{"customer_id": 1, "country": "FR"},
{"customer_id": 2, "country": "FR"},
{"customer_id": 3, "country": "DE"}
])
),
ord := INLINE_COLUMNS(
order_id BIGINT#orders BIND TO order_id,
customer_id BIGINT#customers,
amount BIGINT
FROM JSON([
{"order_id": 10, "customer_id": 1, "amount": 30},
{"order_id": 11, "customer_id": 1, "amount": 50},
{"order_id": 12, "customer_id": 2, "amount": 40},
{"order_id": 13, "customer_id": 3, "amount": 70}
])
),
spend := customer_id.RELATED(SUM(ord[amount]) GROUP BY (ord[customer_id])),
order_country := RELATED(cust[country] VIA ord[customer_id]),
country_spend := RELATED(SUM(ord[amount]) ON order_country = cust[country]),
country_customers := RELATED(COUNT() ON peer[country] = cust[country]),
country_avg := country_spend / country_customers
RETURN
customer_id,
spend,
country_avg,
vs_country := spend::DOUBLE - country_avg
FOR
customer_id := BIND_VALUES(ARRAY(1, 2, 3)),
NESTED order_id := BIND_VALUES(ARRAY(10, 11, 12, 13)),
NESTED peer_id := BIND_VALUES(ARRAY(1, 2, 3))
ORDER BY customer_id
;| CUSTOMER_ID BIGINT#CUSTOMERS | SPEND BIGINT | COUNTRY_AVG DOUBLE | VS_COUNTRY DOUBLE |
|---|---|---|---|
| 1 | 80 | 60 | 20 |
| 2 | 40 | 60 | -20 |
| 3 | 70 | 70 | 0 |
Customers 1 and 2 are in France and spent 120 together, so the French average is 60. Customer 3 is alone in Germany.
The average divides by every customer of the country, including those with no orders, who count as 0. SQL's AVG(spend) over customers with orders excludes them. For that meaning, count the peers that have orders instead.
Next
Choosing a pattern decides whether these features are read at request time or precomputed. Porting batch features moves them from batch sources to serving sources.