Ever tried to build a semantic search for a product catalog and ended up with results that feel like they were picked by a blindfolded monkey? I've been there. Recently, I was working on a men's shirt recommendation engine for a side project, and the struggle was real. The goal was simple: help users find the perfect casual shirt based on fit, fabric, and styleโnot just keywords.
The problem with traditional search is it's brittle. If someone types "breathable linen button-down for summer weddings," the database might just look for "linen" and "button-down" and call it a day. But what about "airy fabric for hot days"? Or "smart casual wedding guest"? That's where vector embeddings and cosine similarity come in.
I used a lightweight approach with sentence-transformers and scikit-learn. Here's the core idea: convert every product description into a vector, then also convert the user's query into a vector. The magic is finding the closest matches.
from sentence_transformers import SentenceTransformer
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np
# Sample product data (descriptions from a men's shirts collection)
products = [
"100% organic cotton oxford shirt, button-down collar, pre-washed for softness",
"Linen blend casual shirt with a relaxed fit and chest pocket, perfect for beach days",
"Slim fit performance polo, moisture-wicking fabric for office or golf",
"Classic chambray shirt, durable yet breathable, ideal for layering"
]
# Load a lightweight model
model = SentenceTransformer('all-MiniLM-L6-v2')
# Encode product descriptions
product_embeddings = model.encode(products)
# User query
query = "Breathable linen shirt for a summer wedding"
query_embedding = model.encode([query])
# Compute similarity
similarities = cosine_similarity(query_embedding, product_embeddings)[0]
# Get top result
best_match_idx = np.argmax(similarities)
print(f"Best match: {products[best_match_idx]} with score {similarities[best_match_idx]:.2f}")
When I ran this, it correctly picked the linen blend shirtโeven though the query had words like "summer wedding" that weren't in the description. The model understood semantic similarity.
Now, I'm building this for a real catalog (like the one at frishay.com/collections/men-shirts), and the results are night and day. Instead of keyword stuffing, the search now understands that "smart casual" and "button-down oxford" are related concepts.
The takeaway? Vector search isn't just for big tech. With a few lines of Python, you can make any product discovery feel intelligent. Next time you're building a search or recommendation system, skip the regex hell and let embeddings do the heavy lifting. Your users will thank you.
Top comments (2)
Interesting how an empty post can spark so much curiosity. What were you hoping to get across with this?
Silence here too, but maybe that's the point. Sometimes the most valuable contributions are the ones that make us stop and think.