Skip to main content

Creating Personalized Filters

Shaped allows you to set item filters that define extra business logic about what items should be returned in each rank request. We support two types of filters:

  1. Personal filters
  2. Global filters

Personal Filters

Personal filters define the items that should be filtered out when ranking for a specific user. For example, you may want your ranking algorithms to:

  • Filter out all videos a user has watched before
  • Remove posts from blocked and muted users
  • Remove items which are not available in the user's country

These are constraints that can't be learned by our machine-learning models and must be specified explicitly by you. Shaped provides another fetch, select query called personal_filters, which works much like the events, users and items queries. The query must output a view containing user_id and item_id pairs that should be filtered out. Specifically, if there is a user_id, item_id row, then that user_id will never be shown that item_id.

Extending the initial video recommendation model from the Your First Model guide, we can add personal filters for all clicked videos below:

video_recommendation_with_personal_filter.yaml
model:
name: video_recommendation_with_personal_filter
description: My first model
connectors:
- type: Dataset
id: click_events
name: click_events
fetch:
events: |
SELECT user_id, item_id, created_at, (CASE WHEN event = 'click' THEN 1 ELSE 0 END) as label
FROM click_events
personal_filters: |
SELECT user_id, item_id FROM click_events where event = 'click'
shaped create-model --file video_recommendation_with_personal_filter.yaml

When making a rank request, you'll now notice that all item_id's paired with a query user_id in the personal_filters query will be filtered out. The great thing is that these filters will be continually fetched, keeping them up-to-date as your users continue to click more videos.

Global Filters

Global filters define the items that should be filtered out when ranking for all users. For example, you may want your rankings to guarantee that all of the following items are filtered out:

  • Products that are out of stock
  • Unsafe content
  • Older items

Shaped provides a global_filters fetch query for this. It defines a view of item_ids that should be filtered for all users when ranking.

Continuing on with the video recommendation example, imagine you have an is_unsafe field on your video table, that you've derived from a moderation reporting system. You can extend your create model query to filter all items with is_unsafe = 1 as follows:

video_recommendation_with_global_filter.yaml
model:
name: video_recommendation_with_global_filter
connectors:
- type: BigQuery
id: bigquery_connector
location: us-west1
project_id: rocket-ship-234123
dataset: video_db
fetch:
events: |
SELECT user_id, item_id, created_at, (CASE WHEN event = 'click' THEN 1 ELSE 0 END) as label
FROM bigquery_connector.click_events
global_filters: |
SELECT v.item_id FROM bigquery_connector.videos as v where v.is_unsafe = 1
shaped create-model --file video_recommendation_with_global_filter.yaml

Next Steps

We've now demonstrated how you can use each of the fetch queries to add events, users, items, personal filters and global filters. These can all be used in combinations to define your exact data model and use-case business logic.

Now that we've gone over the key components of creating a model, we recommend taking a look at the guides on how to manage your models, and use different parts of the rank API, including metadata filtering, similar item and user ranking and more.