Recommender
Recommend items and find similar items from user interaction data
- Connector software
- Free and open source
- Service credentials
- Not needed
- Query Farm hosting
- Available · $10/month organization minimum
Use with an agent
Install the Orchard skill in your project to help your agent find connectors, configure them, and verify SQL results.
npx skills add Query-farm/vgi-skills --skill orchard
Copy this request into your coding agent to get started.
Use https://query.farm/products/orchard/connectors/recommender/index.md to help me query Recommender with DuckDB or Haybarn 2.0.1 and VGI protocol 3.4. Ask only for missing details about the result I need or whether I want local execution, self-hosting or Query Farm hosting. Check the connector's actual hosting availability, credentials and input requirements. Read one format of each needed reference and the exact object declarations before writing SQL. Use a released connector, and distinguish a saved example result from anything you actually execute. If you cannot run the query, tell me what remains to verify.
Recommend the top three items for each user
Example output · captured
- Connector release
- v0.5.0
- Engine
- 2.0.1
- VGI protocol
- 3.4.0
SELECT user, item, rank
FROM recommender.main.recommend_all((SELECT * FROM (VALUES ('u1','A',1.0),('u1','B',1.0),('u1','C',1.0),('u2','A',1.0),('u2','B',1.0),('u2','C',1.0),('u3','A',1.0),('u3','B',1.0),('u4','A',1.0),('u4','B',1.0)) AS events(u, i, v)), user := 'u', item := 'i', value := 'v', n := 3)
ORDER BY user, rank
| user | item | rank |
|---|---|---|
| u3 | C | 1 |
| u4 | C | 1 |
Quick start
Choose where to run Recommender, then follow the steps below.
The connector runs alongside DuckDB or Haybarn. No Query Farm account is needed.
1. Install the vgi extension
Run this in your SQL client. On DuckDB, keep FROM query_farm on LOAD in later sessions.
SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
WITH PREFIX 'https://duckdb-extensions.query.farm';
INSTALL vgi FROM query_farm;
LOAD vgi FROM query_farm;
INSTALL vgi FROM community;
LOAD vgi;
2. Attach the connector
Attach with uvx
Needs uv. uvx builds the worker from its release tag on first attach and reuses the cached environment after that.
ATTACH 'recommender' AS recommender (
TYPE vgi,
LOCATION 'uvx --from git+https://github.com/Query-farm/[email protected] vgi-recommender'
);
Attach the signed image
Needs Docker, Podman or another OCI runtime. The extension runs the container and talks to it over stdio.
ATTACH 'recommender' AS recommender (
TYPE vgi,
LOCATION 'oci://ghcr.io/query-farm/vgi-recommender:0.5.0'
);
3. Run a query
SELECT user, item, rank
FROM recommender.main.recommend_all((SELECT * FROM (VALUES ('u1','A',1.0),('u1','B',1.0),('u1','C',1.0),('u2','A',1.0),('u2','B',1.0),('u2','C',1.0),('u3','A',1.0),('u3','B',1.0),('u4','A',1.0),('u4','B',1.0)) AS events(u, i, v)), user := 'u', item := 'i', value := 'v', n := 3)
ORDER BY user, rank
Run the connector on a server you manage. You operate the service and choose who can connect.
1. Deploy the connector on your server
Follow the connector’s deployment instructions. Use its supported HTTP transport, or launch it remotely over SSH. The connection guide explains both options.
2. Install the vgi extension
Run this in your SQL client. On DuckDB, keep FROM query_farm on LOAD in later sessions.
SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
WITH PREFIX 'https://duckdb-extensions.query.farm';
INSTALL vgi FROM query_farm;
LOAD vgi FROM query_farm;
INSTALL vgi FROM community;
LOAD vgi;
3. Attach the connector
For an HTTP deployment, replace the example URL with your endpoint. Add the authentication options your deployment requires. For SSH, use the SSH connection instructions.
ATTACH 'recommender' AS recommender
(TYPE vgi, LOCATION 'https://your-connector.example.com');
4. Run a query
SELECT user, item, rank
FROM recommender.main.recommend_all((SELECT * FROM (VALUES ('u1','A',1.0),('u1','B',1.0),('u1','C',1.0),('u2','A',1.0),('u2','B',1.0),('u2','C',1.0),('u3','A',1.0),('u3','B',1.0),('u4','A',1.0),('u4','B',1.0)) AS events(u, i, v)), user := 'u', item := 'i', value := 'v', n := 3)
ORDER BY user, rank
Query Farm operates Recommender for your team. Hosting has a $10 monthly minimum per organization; usage counts toward it. From $2.97 per instance-month ($10 monthly minimum).
1. Create your hosted connector
Open the console, choose your organization and deployment size, and add any service credentials there.
2. Install the vgi extension
SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
WITH PREFIX 'https://duckdb-extensions.query.farm';
INSTALL vgi FROM query_farm;
LOAD vgi FROM query_farm;
INSTALL vgi FROM community;
LOAD vgi;
3. Attach your deployment
Copy the ATTACH statement from your deployment in the console and run it in DuckDB or Haybarn. Use recommender as the catalog alias for the query below.
4. Run a query
SELECT user, item, rank
FROM recommender.main.recommend_all((SELECT * FROM (VALUES ('u1','A',1.0),('u1','B',1.0),('u1','C',1.0),('u2','A',1.0),('u2','B',1.0),('u2','C',1.0),('u3','A',1.0),('u3','B',1.0),('u4','A',1.0),('u4','B',1.0)) AS events(u, i, v)), user := 'u', item := 'i', value := 'v', n := 3)
ORDER BY user, rank
Technical details
- Verified connector release
- v0.5.0
- Engine
- 2.0.1
- VGI protocol
- 3.4.0
- Verified on
- December 1, 2026
Pin a version for production
When running the connector yourself, keep a specific release until you’re ready to upgrade. Replace the ATTACH statement above with this one and follow the same extension and credential setup.
Attach with uvx
Needs uv. uvx builds the worker from its release tag on first attach and reuses the cached environment after that.
ATTACH 'recommender' AS recommender (
TYPE vgi,
LOCATION 'uvx --from git+https://github.com/Query-farm/[email protected] vgi-recommender'
);
Attach the signed image
Needs Docker, Podman or another OCI runtime. The extension runs the container and talks to it over stdio.
ATTACH 'recommender' AS recommender (
TYPE vgi,
LOCATION 'oci://ghcr.io/query-farm/vgi-recommender:0.5.0'
);
Overview
Collaborative-filtering recommendation worker (implicit-feedback ALS) for DuckDB via VGI.
Generate personalized product and content recommendations directly in DuckDB SQL with implicit-feedback Alternating Least Squares (ALS) collaborative filtering -- no separate ML pipeline, notebook, or model server required.
This VGI worker turns a single relation of (user, item, value) interactions -- clicks, purchases, plays, ratings, or any engagement signal -- into top-N recommendations, item-to-item similarity, and per-user suggestions. It is built for data engineers, analysts, and application developers who want a fast, reproducible recommender system they can call from a query instead of standing up a dedicated service. Because it runs as a DuckDB attachment, you can join recommendations straight back to your catalog tables, filter them, and ship them to downstream tables or dashboards in a single statement.
Under the hood the worker fits an implicit-feedback ALS matrix-factorization model with the implicit library by Ben Frederickson -- a fast, Cython/BLAS-accelerated implementation of the classic ALS algorithm for implicit datasets. Each call buffers the whole interaction relation, learns latent user and item factors, and scores candidates by the dot product (recommendations) or the cosine of the item factors (similarity). Results are deterministic (pinned random_state, single-threaded BLAS), and value is treated as the interaction confidence, defaulting to 1.0 when the value column is omitted. See the implicit documentation and the collaborative filtering overview for background on the method.
Function surface
The worker covers the three everyday collaborative-filtering workloads: personalized top-N recommendations for the whole user base at once, on-demand recommendations for a single target user, and item-to-item nearest-neighbour lookups (the classic "people who liked X also liked Y" / related-items surface).
Every recommendation function takes the interaction relation as a positional (user, item, value) subquery (Arg(0)) and accepts the column roles and hyperparameters as named arguments (user := ..., item := ..., value := ..., n := ..., factors := ...). The relation's columns are the data; the named arguments just declare which column plays the user, item, and confidence role, so the same query works over any interaction table by pointing the roles at its columns.
Catalog
Everything recommender exposes once attached — read straight from the worker's
released build.
main 1 view · 3 functions
Recommendation table functions: recommend_all, similar_items, recommend_for.
Schema examples (6)
Top-3 recommended items per user (recommend_all), ranked best-first.
SELECT user, item, rank
FROM recommender.main.recommend_all((SELECT * FROM (VALUES ('u1','A',1.0),('u1','B',1.0),('u1','C',1.0),('u2','A',1.0),('u2','B',1.0),('u2','C',1.0),('u3','A',1.0),('u3','B',1.0),('u4','A',1.0),('u4','B',1.0)) AS events(u, i, v)), user := 'u', item := 'i', value := 'v', n := 3)
ORDER BY user, rank;
| user | item | rank |
|---|---|---|
| u3 | C | 1 |
| u4 | C | 1 |
Top-2 nearest-neighbour items per item (similar_items) by ALS cosine.
SELECT item, similar_item, rank
FROM recommender.main.similar_items((SELECT * FROM (VALUES ('u1','A',1.0),('u1','B',1.0),('u1','C',1.0),('u2','A',1.0),('u2','B',1.0),('u2','C',1.0),('u3','A',1.0),('u3','B',1.0),('u4','A',1.0),('u4','B',1.0)) AS events(u, i, v)), user := 'u', item := 'i', value := 'v', n := 2)
ORDER BY item, rank;
Top-2 recommendations for one target user (recommend_for), ranked best-first.
SELECT item, rank
FROM recommender.main.recommend_for((SELECT * FROM (VALUES ('u1','A',1.0),('u1','B',1.0),('u1','C',1.0),('u2','A',1.0),('u2','B',1.0),('u2','C',1.0),('u3','A',1.0),('u3','B',1.0),('u4','A',1.0),('u4','B',1.0)) AS events(u, i, v)), user := 'u', item := 'i', value := 'v', target_user := 'u3', n := 2)
ORDER BY rank;
Explain each viewer's top recommendation (recommend_all) with the title they already watched that is most similar to it (similar_items), e.g. cal gets Matrix because of Dune.
WITH plays AS (SELECT * FROM (VALUES ('ana','Alien',3.0),('ana','Dune',2.0),('ana','Matrix',4.0),('ben','Alien',1.0),('ben','Dune',5.0),('ben','Matrix',2.0),('ben','Blade Runner',3.0),('cal','Dune',2.0),('cal','Blade Runner',4.0),('dee','Notebook',5.0),('dee','Titanic',2.0),('dee','Amelie',3.0),('eve','Notebook',2.0),('eve','Titanic',4.0),('fay','Titanic',3.0),('fay','Amelie',2.0),('fay','Alien',1.0)) AS t(viewer, title, n_plays)), recs AS (SELECT user AS viewer, item AS title FROM recommender.main.recommend_all((SELECT viewer, title, n_plays FROM plays), user := 'viewer', item := 'title', value := 'n_plays', n := 1)), sims AS (SELECT item, similar_item, similarity FROM recommender.main.similar_items((SELECT viewer, title, n_plays FROM plays), user := 'viewer', item := 'title', value := 'n_plays', n := 10)) SELECT r.viewer, r.title AS recommended, arg_max(s.similar_item, s.similarity) AS because_you_watched, round(max(s.similarity), 3) AS similarity
FROM recs r
JOIN plays p ON p.viewer = r.viewer
JOIN sims s ON s.item = r.title AND s.similar_item = p.title
GROUP BY r.viewer, r.title
ORDER BY r.viewer;
2 more examples
Score viewer eve's top-3 ALS recommendations (recommend_for) by their summed item-item similarity to the titles eve watched (similar_items) -- a second, item-based signal to compare or blend with the ALS ranking.
WITH plays AS (SELECT * FROM (VALUES ('ana','Alien',3.0),('ana','Dune',2.0),('ana','Matrix',4.0),('ben','Alien',1.0),('ben','Dune',5.0),('ben','Matrix',2.0),('ben','Blade Runner',3.0),('cal','Dune',2.0),('cal','Blade Runner',4.0),('dee','Notebook',5.0),('dee','Titanic',2.0),('dee','Amelie',3.0),('eve','Notebook',2.0),('eve','Titanic',4.0),('fay','Titanic',3.0),('fay','Amelie',2.0),('fay','Alien',1.0)) AS t(viewer, title, n_plays)), recs AS (SELECT item AS title, score, rank FROM recommender.main.recommend_for((SELECT viewer, title, n_plays FROM plays), user := 'viewer', item := 'title', value := 'n_plays', target_user := 'eve', n := 3)), sims AS (SELECT item, similar_item, similarity FROM recommender.main.similar_items((SELECT viewer, title, n_plays FROM plays), user := 'viewer', item := 'title', value := 'n_plays', n := 10)) SELECT r.rank, r.title, round(r.score, 3) AS als_score, round(sum(s.similarity), 3) AS item_similarity_support
FROM recs r
JOIN plays p ON p.viewer = 'eve'
JOIN sims s ON s.item = r.title AND s.similar_item = p.title
GROUP BY r.rank, r.title, r.score
ORDER BY r.rank;
Top recommendation per user from the built-in sample dataset (recommend_all over sample_interactions), joined back to the dataset to count how many users already interacted with the recommended item: u3 and u4 both get C, which 2 users already have.
WITH recs AS (SELECT "user", item FROM recommender.main.recommend_all((SELECT * FROM recommender.main.sample_interactions), n := 1)) SELECT r."user", r.item, count(DISTINCT s."user") AS users_already_interacted
FROM recs r
JOIN recommender.main.sample_interactions s ON s.item = r.item
GROUP BY r."user", r.item
ORDER BY r."user";
sample_interactions
Built-in sample (user, item, value) interaction dataset to browse and feed to the functions.
Count how many interactions each user has in the sample dataset.
SELECT "user", count(*) AS interactions
FROM recommender.main.sample_interactions
GROUP BY 1
ORDER BY 1;
Feed the sample dataset straight into recommend_all (default role args).
SELECT "user", item, rank
FROM recommender.main.recommend_all((SELECT * FROM recommender.main.sample_interactions), n := 3)
ORDER BY 1, rank;
Top recommendation per user from the built-in sample dataset (recommend_all over sample_interactions), joined back to the dataset to count how many users already interacted with the recommended item: u3 and u4 both get C, which 2 users already have.
WITH recs AS (SELECT "user", item FROM recommender.main.recommend_all((SELECT * FROM recommender.main.sample_interactions), n := 1)) SELECT r."user", r.item, count(DISTINCT s."user") AS users_already_interacted
FROM recs r
JOIN recommender.main.sample_interactions s ON s.item = r.item
GROUP BY r."user", r.item
ORDER BY r."user";
1 more example
Recommend the novel item C to users u3 and u4 from the sample dataset.
SELECT "user", item
FROM recommender.main.recommend_all((SELECT * FROM recommender.main.sample_interactions), n := 1)
WHERE "user" IN ('u3', 'u4')
ORDER BY 1;
recommend_all (data TABLE, factors := BIGINT, item := VARCHAR, n := BIGINT, user := VARCHAR, value := VARCHAR)
Collaborative-filtering: fit implicit-feedback ALS, emit top-N recommended items per user (excluding already-seen items) as (user, item, score, rank). value is the interaction strength (confidence); defaults to 1.0 if absent.
Top-3 recommendations per user over an inline interaction relation.
SELECT user, item, rank
FROM recommender.main.recommend_all((SELECT * FROM (VALUES ('u1','A',1.0),('u1','B',1.0),('u1','C',1.0),('u2','A',1.0),('u2','B',1.0),('u2','C',1.0),('u3','A',1.0),('u3','B',1.0),('u4','A',1.0),('u4','B',1.0)) AS events(u, i, v)), user := 'u', item := 'i', value := 'v', n := 3)
ORDER BY user, rank;
| user | item | rank |
|---|---|---|
| u3 | C | 1 |
| u4 | C | 1 |
Recommend novel item C to users u3 and u4 (planted CF signal).
SELECT user, item
FROM recommender.main.recommend_all((SELECT * FROM (VALUES ('u1','A',1.0),('u1','B',1.0),('u1','C',1.0),('u2','A',1.0),('u2','B',1.0),('u2','C',1.0),('u3','A',1.0),('u3','B',1.0),('u4','A',1.0),('u4','B',1.0)) AS events(u, i, v)), user := 'u', item := 'i', value := 'v', n := 1)
WHERE user IN ('u3', 'u4')
ORDER BY user;
Explain each viewer's top recommendation (recommend_all) with the title they already watched that is most similar to it (similar_items), e.g. cal gets Matrix because of Dune.
WITH plays AS (SELECT * FROM (VALUES ('ana','Alien',3.0),('ana','Dune',2.0),('ana','Matrix',4.0),('ben','Alien',1.0),('ben','Dune',5.0),('ben','Matrix',2.0),('ben','Blade Runner',3.0),('cal','Dune',2.0),('cal','Blade Runner',4.0),('dee','Notebook',5.0),('dee','Titanic',2.0),('dee','Amelie',3.0),('eve','Notebook',2.0),('eve','Titanic',4.0),('fay','Titanic',3.0),('fay','Amelie',2.0),('fay','Alien',1.0)) AS t(viewer, title, n_plays)), recs AS (SELECT user AS viewer, item AS title FROM recommender.main.recommend_all((SELECT viewer, title, n_plays FROM plays), user := 'viewer', item := 'title', value := 'n_plays', n := 1)), sims AS (SELECT item, similar_item, similarity FROM recommender.main.similar_items((SELECT viewer, title, n_plays FROM plays), user := 'viewer', item := 'title', value := 'n_plays', n := 10)) SELECT r.viewer, r.title AS recommended, arg_max(s.similar_item, s.similarity) AS because_you_watched, round(max(s.similarity), 3) AS similarity
FROM recs r
JOIN plays p ON p.viewer = r.viewer
JOIN sims s ON s.item = r.title AND s.similar_item = p.title
GROUP BY r.viewer, r.title
ORDER BY r.viewer;
recommend_for (data TABLE, factors := BIGINT, item := VARCHAR, n := BIGINT, target_user := VARCHAR, user := VARCHAR, value := VARCHAR)
Top-N recommended items for a single specified user, excluding items that user has already interacted with, as (item, score, rank). Returns no rows when the requested user is absent from the interaction relation.
Top-2 recommendations for a single target user (u3).
SELECT item, rank
FROM recommender.main.recommend_for((SELECT * FROM (VALUES ('u1','A',1.0),('u1','B',1.0),('u1','C',1.0),('u2','A',1.0),('u2','B',1.0),('u2','C',1.0),('u3','A',1.0),('u3','B',1.0),('u4','A',1.0),('u4','B',1.0)) AS events(u, i, v)), user := 'u', item := 'i', value := 'v', target_user := 'u3', n := 2)
ORDER BY rank;
Score viewer eve's top-3 ALS recommendations (recommend_for) by their summed item-item similarity to the titles eve watched (similar_items) -- a second, item-based signal to compare or blend with the ALS ranking.
WITH plays AS (SELECT * FROM (VALUES ('ana','Alien',3.0),('ana','Dune',2.0),('ana','Matrix',4.0),('ben','Alien',1.0),('ben','Dune',5.0),('ben','Matrix',2.0),('ben','Blade Runner',3.0),('cal','Dune',2.0),('cal','Blade Runner',4.0),('dee','Notebook',5.0),('dee','Titanic',2.0),('dee','Amelie',3.0),('eve','Notebook',2.0),('eve','Titanic',4.0),('fay','Titanic',3.0),('fay','Amelie',2.0),('fay','Alien',1.0)) AS t(viewer, title, n_plays)), recs AS (SELECT item AS title, score, rank FROM recommender.main.recommend_for((SELECT viewer, title, n_plays FROM plays), user := 'viewer', item := 'title', value := 'n_plays', target_user := 'eve', n := 3)), sims AS (SELECT item, similar_item, similarity FROM recommender.main.similar_items((SELECT viewer, title, n_plays FROM plays), user := 'viewer', item := 'title', value := 'n_plays', n := 10)) SELECT r.rank, r.title, round(r.score, 3) AS als_score, round(sum(s.similarity), 3) AS item_similarity_support
FROM recs r
JOIN plays p ON p.viewer = 'eve'
JOIN sims s ON s.item = r.title AND s.similar_item = p.title
GROUP BY r.rank, r.title, r.score
ORDER BY r.rank;
similar_items (data TABLE, factors := BIGINT, item := VARCHAR, n := BIGINT, user := VARCHAR, value := VARCHAR)
Item-item collaborative filtering: top-N most similar items per item by cosine of the ALS item factors, as (item, similar_item, similarity, rank).
Top-2 most similar items for every item in an inline relation.
SELECT item, similar_item, rank
FROM recommender.main.similar_items((SELECT * FROM (VALUES ('u1','A',1.0),('u1','B',1.0),('u1','C',1.0),('u2','A',1.0),('u2','B',1.0),('u2','C',1.0),('u3','A',1.0),('u3','B',1.0),('u4','A',1.0),('u4','B',1.0)) AS events(u, i, v)), user := 'u', item := 'i', value := 'v', n := 2)
ORDER BY item, rank;
Explain each viewer's top recommendation (recommend_all) with the title they already watched that is most similar to it (similar_items), e.g. cal gets Matrix because of Dune.
WITH plays AS (SELECT * FROM (VALUES ('ana','Alien',3.0),('ana','Dune',2.0),('ana','Matrix',4.0),('ben','Alien',1.0),('ben','Dune',5.0),('ben','Matrix',2.0),('ben','Blade Runner',3.0),('cal','Dune',2.0),('cal','Blade Runner',4.0),('dee','Notebook',5.0),('dee','Titanic',2.0),('dee','Amelie',3.0),('eve','Notebook',2.0),('eve','Titanic',4.0),('fay','Titanic',3.0),('fay','Amelie',2.0),('fay','Alien',1.0)) AS t(viewer, title, n_plays)), recs AS (SELECT user AS viewer, item AS title FROM recommender.main.recommend_all((SELECT viewer, title, n_plays FROM plays), user := 'viewer', item := 'title', value := 'n_plays', n := 1)), sims AS (SELECT item, similar_item, similarity FROM recommender.main.similar_items((SELECT viewer, title, n_plays FROM plays), user := 'viewer', item := 'title', value := 'n_plays', n := 10)) SELECT r.viewer, r.title AS recommended, arg_max(s.similar_item, s.similarity) AS because_you_watched, round(max(s.similarity), 3) AS similarity
FROM recs r
JOIN plays p ON p.viewer = r.viewer
JOIN sims s ON s.item = r.title AND s.similar_item = p.title
GROUP BY r.viewer, r.title
ORDER BY r.viewer;
Score viewer eve's top-3 ALS recommendations (recommend_for) by their summed item-item similarity to the titles eve watched (similar_items) -- a second, item-based signal to compare or blend with the ALS ranking.
WITH plays AS (SELECT * FROM (VALUES ('ana','Alien',3.0),('ana','Dune',2.0),('ana','Matrix',4.0),('ben','Alien',1.0),('ben','Dune',5.0),('ben','Matrix',2.0),('ben','Blade Runner',3.0),('cal','Dune',2.0),('cal','Blade Runner',4.0),('dee','Notebook',5.0),('dee','Titanic',2.0),('dee','Amelie',3.0),('eve','Notebook',2.0),('eve','Titanic',4.0),('fay','Titanic',3.0),('fay','Amelie',2.0),('fay','Alien',1.0)) AS t(viewer, title, n_plays)), recs AS (SELECT item AS title, score, rank FROM recommender.main.recommend_for((SELECT viewer, title, n_plays FROM plays), user := 'viewer', item := 'title', value := 'n_plays', target_user := 'eve', n := 3)), sims AS (SELECT item, similar_item, similarity FROM recommender.main.similar_items((SELECT viewer, title, n_plays FROM plays), user := 'viewer', item := 'title', value := 'n_plays', n := 10)) SELECT r.rank, r.title, round(r.score, 3) AS als_score, round(sum(s.similarity), 3) AS item_similarity_support
FROM recs r
JOIN plays p ON p.viewer = 'eve'
JOIN sims s ON s.item = r.title AND s.similar_item = p.title
GROUP BY r.rank, r.title, r.score
ORDER BY r.rank;
Try these queries
Top-2 nearest-neighbour items per item (similar_items) by ALS cosine.
SELECT item, similar_item, rank
FROM recommender.main.similar_items((SELECT * FROM (VALUES ('u1','A',1.0),('u1','B',1.0),('u1','C',1.0),('u2','A',1.0),('u2','B',1.0),('u2','C',1.0),('u3','A',1.0),('u3','B',1.0),('u4','A',1.0),('u4','B',1.0)) AS events(u, i, v)), user := 'u', item := 'i', value := 'v', n := 2)
ORDER BY item, rank
Uses similar_items
Top-2 recommendations for one target user (recommend_for), ranked best-first.
SELECT item, rank
FROM recommender.main.recommend_for((SELECT * FROM (VALUES ('u1','A',1.0),('u1','B',1.0),('u1','C',1.0),('u2','A',1.0),('u2','B',1.0),('u2','C',1.0),('u3','A',1.0),('u3','B',1.0),('u4','A',1.0),('u4','B',1.0)) AS events(u, i, v)), user := 'u', item := 'i', value := 'v', target_user := 'u3', n := 2)
ORDER BY rank
Uses recommend_for
Explain each viewer's top recommendation (recommend_all) with the title they already watched that is most similar to it (similar_items), e.g. cal gets Matrix because of Dune.
WITH plays AS (SELECT * FROM (VALUES ('ana','Alien',3.0),('ana','Dune',2.0),('ana','Matrix',4.0),('ben','Alien',1.0),('ben','Dune',5.0),('ben','Matrix',2.0),('ben','Blade Runner',3.0),('cal','Dune',2.0),('cal','Blade Runner',4.0),('dee','Notebook',5.0),('dee','Titanic',2.0),('dee','Amelie',3.0),('eve','Notebook',2.0),('eve','Titanic',4.0),('fay','Titanic',3.0),('fay','Amelie',2.0),('fay','Alien',1.0)) AS t(viewer, title, n_plays)), recs AS (SELECT user AS viewer, item AS title FROM recommender.main.recommend_all((SELECT viewer, title, n_plays FROM plays), user := 'viewer', item := 'title', value := 'n_plays', n := 1)), sims AS (SELECT item, similar_item, similarity FROM recommender.main.similar_items((SELECT viewer, title, n_plays FROM plays), user := 'viewer', item := 'title', value := 'n_plays', n := 10)) SELECT r.viewer, r.title AS recommended, arg_max(s.similar_item, s.similarity) AS because_you_watched, round(max(s.similarity), 3) AS similarity
FROM recs r
JOIN plays p ON p.viewer = r.viewer
JOIN sims s ON s.item = r.title AND s.similar_item = p.title
GROUP BY r.viewer, r.title
ORDER BY r.viewer
Score viewer eve's top-3 ALS recommendations (recommend_for) by their summed item-item similarity to the titles eve watched (similar_items) -- a second, item-based signal to compare or blend with the ALS ranking.
WITH plays AS (SELECT * FROM (VALUES ('ana','Alien',3.0),('ana','Dune',2.0),('ana','Matrix',4.0),('ben','Alien',1.0),('ben','Dune',5.0),('ben','Matrix',2.0),('ben','Blade Runner',3.0),('cal','Dune',2.0),('cal','Blade Runner',4.0),('dee','Notebook',5.0),('dee','Titanic',2.0),('dee','Amelie',3.0),('eve','Notebook',2.0),('eve','Titanic',4.0),('fay','Titanic',3.0),('fay','Amelie',2.0),('fay','Alien',1.0)) AS t(viewer, title, n_plays)), recs AS (SELECT item AS title, score, rank FROM recommender.main.recommend_for((SELECT viewer, title, n_plays FROM plays), user := 'viewer', item := 'title', value := 'n_plays', target_user := 'eve', n := 3)), sims AS (SELECT item, similar_item, similarity FROM recommender.main.similar_items((SELECT viewer, title, n_plays FROM plays), user := 'viewer', item := 'title', value := 'n_plays', n := 10)) SELECT r.rank, r.title, round(r.score, 3) AS als_score, round(sum(s.similarity), 3) AS item_similarity_support
FROM recs r
JOIN plays p ON p.viewer = 'eve'
JOIN sims s ON s.item = r.title AND s.similar_item = p.title
GROUP BY r.rank, r.title, r.score
ORDER BY r.rank
Top recommendation per user from the built-in sample dataset (recommend_all over sample_interactions), joined back to the dataset to count how many users already interacted with the recommended item: u3 and u4 both get C, which 2 users already have.
WITH recs AS (SELECT "user", item FROM recommender.main.recommend_all((SELECT * FROM recommender.main.sample_interactions), n := 1)) SELECT r."user", r.item, count(DISTINCT s."user") AS users_already_interacted
FROM recs r
JOIN recommender.main.sample_interactions s ON s.item = r.item
GROUP BY r."user", r.item
ORDER BY r."user"
Count how many interactions each user has in the sample dataset.
SELECT "user", count(*) AS interactions
FROM recommender.main.sample_interactions
GROUP BY 1
ORDER BY 1
Uses sample_interactions
3 more examples in the Catalog, each listed under the table or function it uses.
Recommender is an independent open-source connector from Query.Farm. It is not affiliated with, endorsed by or sponsored by the services, projects or organisations it works with. Product names, logos and trademarks belong to their respective owners.
Related workers
Anomaly
Statistics & ML
Find outliers, discords, repeated motifs and change points in numeric series from SQL
Causal
Statistics & ML
Estimate treatment effects (ATE, ATT) and propensity scores from observational data with IPW, regression adjustment, and AIPW
Explain
Statistics & ML
Explain scikit-learn and XGBoost model predictions with per-row SHAP values and global feature importance