Prompt
I’m building a similar-items API for a catalog app
Latest observation
Great use case. A “similar items” API usually works best when it’s designed around candidate generation + ranking + explainability + freshness.
Here’s a practical blueprint.
1) Define “similar”
Before architecture, decide what “similar” means for your app. Common dimensions:
- Visual similarity: same style, color, shape, pattern
- Attribute similarity: brand, category, material, size, price band
- Behavioral similarity: users who viewed/bought this also viewed/bought that
- Business similarity: cross-sell, substitute, complementary items
Most catalogs benefit from a hybrid approach.
2) API shape
A simple endpoint could be:
GET /items/{item_id}/similar?limit=12
Optional query params:
limitfilters(category, availability, price range, locale)strategy(hybrid,visual,attribute,behavioral)exclude_out_of_stock=trueexplain=true
Example response:
{
"item_id": "123",
"similar_items": [
{
"id": "456",
"score": 0.92,
"reasons": ["same category", "similar color", "same price band"]
}
]
}
3) Retrieval pipeline
A strong pattern is:
A. Candidate generation
Pull a few hundred or thousand plausible candidates from multiple sources:
- Same category / subcategory
- Vector search over embeddings
- Co-click / co-purchase graph neighbors
- Same brand or style cluster
- Rule-based substitutions
B. Ranking
Score the candidates with a ranking model or weighted heuristic:
- Attribute match score
- Embedding cosine similarity
- Popularity / conversion priors
- Inventory / availability boost
- Price proximity
- Business constraints
C. Re-ranking / diversification
Avoid returning near-duplicates only. Add:
- category diversification
- brand diversification
- color variety
- deduping variant SKUs
4) Data model
You’ll likely want:
Item table
- id
- title
- category
- brand
- price
- color
- material
- size
- availability
- locale
- image URLs
- text description
Feature store
- normalized attributes
- text embeddings
- image embeddings
- popularity metrics
- co-interaction stats
Similarity index
- vector index (FAISS, pgvector, Pinecone, Weaviate, OpenSearch)
- graph edges for behavioral similarity
- inverted indexes for attribute filters
5) Similarity methods
A good production setup often mixes:
Attribute-based
Great when you have structured catalog data.
Example score:
- same category: +0.4
- same brand: +0.1
- price proximity: +0.2
- color similarity: +0.1
- material similarity: +0.1
Embedding-based
Useful for titles, descriptions, images, or both.
- Text embeddings from product title + description + attributes
- Image embeddings from product images
- Combine them into a single vector or separate scores
Behavioral
Good for recommendations that reflect real users.
- Co-view
- Co-cart
- Co-purchase
- Session-based similarity
Hybrid
Usually best:
- 40% attribute
- 40% embeddings
- 20% behavioral
Adjust based on catalog and goals.
6) Handling variants
Important for catalogs with color/size variants:
- Don’t return the same parent product’s variants as “similar” unless desired
- Normalize SKUs into product groups
- Decide whether similarity is at:
- SKU level
- product/SPU level
- variant family level
7) Filters and constraints
Common business rules:
- only in-stock items
- only items sold in user’s region
- exclude discontinued items
- exclude same item / same variant
- enforce minimum rating or quality thresholds
- respect age/brand/legal restrictions
8) Explainability
Very useful for debugging and trust.
Return reasons like:
- “same brand”
- “same category”
- “similar style”
- “often bought together”
- “within 10% price range”
9) Metrics
Offline:
- Precision@K
- Recall@K
- NDCG@K
- MAP
- coverage / diversity
Online:
- CTR
- add-to-cart rate
- conversion rate
- revenue per session
- bounce reduction
Also monitor:
- latency
- cache hit rate
- index freshness
- drift in embeddings or catalog data
10) Suggested implementation path
If you want something practical and incremental:
Phase 1
Rule-based + attribute similarity
- easiest to ship
- deterministic
- explainable
Phase 2
Add text/image embeddings
- better semantic matches
- improves “style similarity”
Phase 3
Add behavioral signals
- most lift for mature catalogs
Phase 4
Learn-to-rank model
- combine all signals
- optimize for CTR or conversions
11) A simple ranking formula
As a starting point:
final_score =
0.35 * attribute_score +
0.35 * embedding_score +
0.20 * behavioral_score +
0.05 * popularity_score +
0.05 * availability_score
Then tune via experiments.
12) Common pitfalls
- Returning duplicates or near-duplicates
- Ignoring availability or regional constraints
- Overweighting popularity so results become generic
- Not separating SKU vs product-level similarity
- Slow vector queries without caching
- No diversification
- No offline evaluation
If you want, I can help with any of these next:
- design the API schema,
- choose a similarity algorithm stack,
- write a database/indexing plan,
- or sketch a production architecture for the service.