Medium system-design machine-learning

How are embeddings used downstream: features, clustering and retrieval?

The 30 second answer

Embeddings are used in three ways. As features, they replace one-hot IDs and transfer knowledge to other models. For clustering, items close together form segments or near-duplicate groups. For retrieval, I embed candidates offline into an approximate nearest-neighbour index, embed the query online, and pass the neighbours to a heavier ranker. Text search usually runs hybrid with keywords, and RAG puts retrieved documents into a prompt.

Features. A product embedding is a compact input for another model. Co-purchase vectors can even help a substitutes model, because substitutes rarely share a cart. In larger companies a shared item embedding is computed once, cached and reused by search, recommendations, ads and moderation; a team with special needs can add an adapter on top.

Clustering. Group items or users by distance: shopper segments, topic discovery, or near-duplicate detection, which is pairs within a distance threshold. With good embeddings plain k-means is often enough.

Retrieval. Embed candidates offline and build an approximate index such as HNSW (a layered proximity graph) or IVF (inverted lists over coarse clusters). Embed the query online and fetch nearest neighbours. The index is there for recall; a ranker or classifier then scores each candidate with the context. Approximate indexes trade some recall for speed, so measure it.

Hybrid text search. Combine embedding matches with keyword matches, because each catches what the other misses. The combination can be a weighted sum, reciprocal rank fusion or a ranker. pgvector documents hybrid search with PostgreSQL full-text search, which makes a cheap baseline. See the separate product search design page for retrieval and ranking in depth.

RAG and HyDE. Retrieval-augmented generation embeds documents and the question, retrieves the top k, and puts them in the prompt. HyDE first asks a language model to write a hypothetical answer document, embeds that document instead of the question, and retrieves real documents near it; the generated text may contain false details, but only real documents are returned. Measure retrieval recall separately from answer quality, so you know whether a bad answer came from missing documents or from the prompt and model.

Sources (4)
  1. Malkov and Yashunin, HNSW
  2. Faiss wiki: index types (IndexHNSWFlat, IndexIVFFlat)
  3. pgvector README: HNSW, IVFFlat and hybrid search
  4. Gao et al., Precise Zero-Shot Dense Retrieval without Relevance Labels (HyDE)