An embedding is a dense float vector, commonly a few hundred to a few thousand dimensions (1024 and 3072 are common), that encodes the meaning of text, an image or code. Nearby vectors mean similar content, measured by cosine similarity.
From input to nearest neighbors
- An embedding model converts the input into a vector.
- You store the vectors in a database such as pgvector, Pinecone or Qdrant.
- A query is embedded the same way.
- An approximate nearest-neighbor search, using an algorithm such as HNSW or IVF, returns the closest stored vectors.
Without embeddings you would rely on keyword matching, which cannot see the meaning behind the words. Storing vectors in a database built for fast retrieval is what makes the search practical.
The model places related inputs near each other, so "dog" and "puppy" get close vectors. A query for "canine" then retrieves "dog", "puppy" and "wolf", all of which sit nearby in the space.
Meaning versus exact terms
A hash table matches exact keys and a full-text index matches words. An embedding matches meaning, so "car" can retrieve "automobile" though the two share no characters. Synonyms and paraphrases that defeat an index are within reach. Because the vectors carry context and intent, retrieval often returns more relevant results for natural-language queries than keyword matching alone.
BM25 and similar keyword methods are strong at finding documents with the exact terms. They miss paraphrases that share no terms with the query.
Hybrid search for IDs, names and rare tokens
Embeddings blur exact matches. For specific IDs, names or rare tokens, keyword search is more precise. Many systems therefore run both: keywords for exact matches and embeddings for intent. Weighting the two by use case works well in practice. The trade-off is that embeddings give richer contextual understanding but can add noise, since they focus on meaning, while keyword search is precise but misses related concepts.
Embeddings are the base of Retrieval-Augmented Generation (RAG) systems and of any feature that needs to find similar items.
| Task | Better fit |
|---|---|
| Semantic search, RAG retrieval | Embeddings |
| Deduplication, clustering, recommendations | Embeddings |
| Exact IDs, names, rare tokens | Keyword search |
Example scenario: retrieval success falls to 65%
Suppose retrieval success drops to 65%. One cause is drift in the embedding space: the data distribution changed, or the embedding model was updated while the stored vectors were not.
- Measure cosine similarity between recent queries and their expected results.
- Log the distribution of embeddings over time and look for shifts that line up with the drop.
- A/B test the current embedding model against a previous version.
If the embedding model changed, the remedy is to re-embed the stored vectors with the current model. If the data shifted, fine-tune the model on current data. Either costs compute. Monitor retrieval accuracy and drift afterwards.