Prompt
How do I build similar item recommendations for an ecommerce site?
Latest observation
To build “similar item” recommendations for an ecommerce site, you generally want a product-to-product similarity system. There are a few ways to do it, and the best choice depends on your data volume, catalog quality, and how personalized you want it.
1) Start with the simplest useful approach
A. Content-based similarity
Recommend products that are similar based on item attributes like:
- category
- brand
- title/description
- price
- color/material
- tags
- embeddings from images/text
How it works:
- Build a feature vector for each product.
- Compute similarity between products.
- For a given product, return the nearest neighbors.
Good for:
- cold start items
- new catalogs
- explainable recommendations
Common methods:
- TF-IDF on text fields
- one-hot encoding for structured attributes
- cosine similarity
- image/text embeddings from ML models
B. Behavioral similarity
Use user behavior to infer which items are related:
- co-views
- co-adds-to-cart
- co-purchases
- “users who viewed this also viewed that”
How it works:
- Build item-item interaction counts.
- Normalize the counts to avoid popularity bias.
- Recommend items often interacted with together.
Good for:
- strong relevance
- discovering non-obvious complements
- improving with more traffic
Common methods:
- item-item collaborative filtering
- matrix factorization
- association rules
- graph-based recommendation
C. Hybrid approach
Usually the best production choice.
Combine:
- content similarity
- behavioral signals
- popularity/trending
- business rules
This handles:
- new items
- sparse traffic
- noisy data
- catalog changes
2) Decide what “similar” means
For ecommerce, “similar” can mean different things:
- Substitutes: same type of product, e.g. two sneakers
- Complements: products bought together, e.g. a camera and memory card
- Style similarity: same aesthetic, e.g. similar dresses
- Functional similarity: solve the same need, e.g. two laptop chargers
This matters because a “similar items” widget often means substitutes, while “frequently bought together” means complements.
3) A practical architecture
Offline pipeline
- Ingest product catalog
- Clean and normalize attributes
- Create item embeddings or features
- Compute nearest neighbors for each item
- Store top N similar items per product
Online serving
- When a product page loads, fetch precomputed similar items
- Apply filters:
- in stock
- same locale
- price constraints
- not already purchased
- brand exclusions if needed
- Re-rank with business rules
This is fast and scalable.
4) How to build content-based recommendations
Step 1: Prepare product data
Example fields:
- title
- description
- category
- brand
- price
- attributes
- image
Step 2: Convert to vectors
Options:
- Text: TF-IDF or transformer embeddings
- Structured features: one-hot or target encoding
- Images: CNN/vision embeddings
- Combine all: concatenate or learn a joint embedding
Step 3: Compute similarity
Use:
- cosine similarity
- Euclidean distance
- approximate nearest neighbors for scale
Step 4: Retrieve top-k similar items
For each product, store the top 20–100 nearest products.
5) How to build behavioral recommendations
Example signals
- Viewed together in the same session
- Bought together in the same order
- Frequently viewed by same users
Simple item-item similarity formula
A common approach is:
- count co-occurrences between items
- normalize by item popularity
For example:
- Jaccard similarity
- cosine similarity on interaction vectors
- lift / PMI for co-purchase strength
This often works very well if you have enough traffic.
6) How to combine content + behavior
A simple hybrid scoring formula might be:
final_score =
- 0.5 × behavioral_similarity
- 0.3 × content_similarity
- 0.1 × popularity
- 0.1 × margin/business_priority
You can tune weights using offline evaluation and A/B tests.
A more advanced setup:
- candidate generation from multiple systems
- rank candidates with a learning-to-rank model
7) Scaling considerations
If your catalog is small:
- brute-force similarity is okay
If your catalog is large:
- use approximate nearest neighbor search:
- FAISS
- Annoy
- HNSW
- ScaNN
Typical flow:
- Create embeddings for all items
- Index them in ANN
- Query nearest neighbors by item id
8) Evaluation metrics
Offline:
- Precision@K
- Recall@K
- MAP / NDCG
- Coverage
- Diversity
- Catalog freshness
Online:
- CTR on recommendation widget
- add-to-cart rate
- conversion rate
- revenue per session
- long-term retention
Always A/B test before full rollout.
9) Practical tips
- Remove out-of-stock items
- Exclude duplicate variants unless you want variant recommendations
- Penalize extremely popular items if they dominate results
- Keep the recommender updated regularly
- Use different logic for:
- product page
- cart page
- checkout page
- homepage
10) A good MVP plan
If you want to ship fast:
- Use product metadata only
- Build text embeddings from title + description
- Add category/brand filters
- Use cosine similarity to get top 20 similar items
- Filter for in-stock and price range
- Measure CTR and refine with purchase/session data later
11) Suggested tech stack
- Data storage: PostgreSQL, BigQuery, Snowflake
- Feature generation: Python, pandas, Spark
- Embeddings: sentence-transformers, CLIP for images
- ANN search: FAISS, HNSWlib
- Serving: FastAPI, Redis cache
- Experimentation: A/B testing platform
If you want, I can also give you:
- a simple Python example for item similarity,
- a production architecture diagram, or
- a recommendation strategy by use case like “similar products” vs “frequently bought together.”