Prompt
How do I use vector search for recommendations?
Latest observation
Vector search is a great way to build recommendations when you can represent items and users as embeddings. The core idea is:
- turn each item into a vector
- turn the user’s current interests into a vector
- find the item vectors closest to the user vector
Basic workflow
1. Create embeddings for items
For each product, article, movie, song, etc., generate an embedding from:
- title
- description
- tags
- category
- metadata
- user-generated text like reviews
Example:
- “wireless noise-canceling headphones”
- “over-ear bluetooth headset with ANC”
These should end up near each other in vector space.
2. Build a user/query vector
You can create a vector for a user from:
- items they clicked
- items they purchased
- search terms
- profile data
- session behavior
Common approach:
- average the embeddings of items they liked
- weight recent interactions more heavily
- combine text from search/history into one embedding
3. Store embeddings in a vector database
Use a vector index such as:
- FAISS
- Milvus
- Pinecone
- Weaviate
- pgvector in Postgres
- Elasticsearch/OpenSearch vector search
4. Retrieve nearest neighbors
For a given user vector, run nearest-neighbor search to find top-k similar items.
This gives you:
- “users who like X also like Y” style recommendations
- personalized “for you” results
- similar item recommendations
5. Filter and rerank
After vector retrieval, apply business rules:
- exclude already purchased items
- filter out out-of-stock items
- enforce freshness/diversity
- rerank using a separate model or heuristics
Simple example
If a user liked:
- “The Martian”
- “Project Hail Mary”
You embed those books, average the vectors, and search for nearby books. Results might include:
- “Artemis”
- “Red Mars”
- “Seveneves”
Common recommendation patterns
Item-to-item recommendations
“Because you viewed this, you might like that.”
- Embed each item
- Find nearest neighbors for the item
User-to-item recommendations
“Recommended for you.”
- Build a user embedding from history
- Search across the item catalog
Session-based recommendations
Use the last few clicks in the current session to build a temporary vector and recommend relevant items.
Why vector search works well
It captures semantic similarity beyond exact keywords:
- “sneakers” and “trainers”
- “laptop” and “notebook computer”
- “mindfulness meditation” and “stress relief breathing”
Important caveats
Vector search alone is usually not enough for production recommendations:
- it may over-recommend similar items
- it doesn’t automatically handle popularity, freshness, or constraints
- it can miss collaborative signals unless embeddings are built from behavior
- it may not be ideal for very sparse catalogs without good features
Best practice
Use vector search as the candidate generation step, then combine it with:
- collaborative filtering
- popularity signals
- business rules
- ranking models
If you want to implement it
A typical stack looks like:
- Embed items with an embedding model
- Index vectors in a vector DB
- Build user/session vectors from interactions
- Search top-k nearest items
- Filter and rerank
- Log clicks/purchases to improve embeddings
If you want, I can show you:
- a concrete Python example using FAISS or pgvector
- how to build user embeddings
- how to combine vector search with collaborative filtering