Suppose a student searches a course website for “how machines recognize photos”. The relevant article might be titled “Computer Vision Fundamentals” and never use the exact search phrase. Keyword matching can miss it. Embedding-based retrieval tries to find material by comparing numerical representations of meaning rather than relying solely on exact words.
An embedding is a numerical vector produced by a model to represent an item such as text, an image, or audio. A vector search system compares these vectors to find nearby items under a chosen similarity measure.
1. From words to vectors
A computer does not directly calculate the semantic similarity of two English paragraphs. An embedding model maps each paragraph to a fixed-length vector:
Here is the embedding dimension. The coordinates are learned features, not human-assigned scores for named concepts. Similar meanings may map to nearby locations in a model’s vector space, though no embedding is perfectly semantic or unbiased.
A toy illustration uses only two dimensions. Real text embeddings typically have far more dimensions.
| Item | Illustrative vector |
|---|---|
| “cat” | |
| “kitten” | |
| “database” |
The values are invented for illustration; they are not measurements from an actual embedding model.
2. Similarity measures
Cosine similarity compares the orientation of two nonzero vectors:
It ranges from to . Higher values indicate more similar directions. A value of means identical direction, but not necessarily identical text or truth.
Euclidean distance measures straight-line distance:
Distance and similarity are not interchangeable. Their meaning depends on the model and whether vectors are normalized. In applications, use the metric recommended by the embedding model and index.
3. Work through a vector-search example
Consider a query vector and three document vectors:
- Course notes on AI:
- Lecture on data storage:
- Tutorial on image recognition:
The cosine similarities are approximately , , and . Under this toy geometry, the AI notes rank first, then image recognition, then data storage. The example demonstrates the ranking operation, not the quality of a trained semantic model.
import numpy as np
query = np.array([1.0, 0.0])
documents = np.array([
[0.9, 0.1],
[0.1, 0.9],
[0.8, 0.2],
])
def normalize(x):
norm = np.linalg.norm(x, axis=-1, keepdims=True)
return x / np.maximum(norm, 1e-12)
scores = normalize(documents) @ normalize(query)
for index in np.argsort(-scores):
print(int(index), round(float(scores[index]), 3))
This implementation performs exact similarity ranking over three vectors. It does not generate embeddings and is not a full vector database.
4. The retrieval pipeline
A common search application has two separate workflows.
Indexing (offline or periodically):
- Collect authorized documents.
- Extract and clean the text.
- Divide long documents into sensible chunks when needed.
- Compute an embedding for each item or chunk.
- Store the vector with its source ID, access permissions, and other metadata.
Querying (on demand):
- Embed the user’s question using a compatible model.
- Find nearby stored vectors.
- Apply permission filters and other constraints.
- Optionally rerank candidates using a more expensive model.
- Display the results or pass selected passages to a generator.
5. Exact and approximate nearest-neighbor search
For a small dataset, the system can compare every stored vector to the query. As collections grow, this becomes expensive. Approximate nearest-neighbor (ANN) indexing trades some recall for faster searches or lower resource costs. Common index families include graph-based and partition-based methods.
Approximate retrieval does not guarantee finding the mathematically closest vector every time. Measure retrieval recall and latency on representative queries before choosing index parameters.
6. Vector, keyword, and hybrid search
Vector search helps with paraphrases and concept similarity. Keyword search can be better for exact names, identifiers, version numbers, and rare strings. Hybrid search combines both, often merging or reranking candidates.
For example, a query about Python 3.13 contains an exact version requirement. A semantically close document about a different Python release may be irrelevant even if its embedding score is high.
7. Important limitations
- Similarity is not factual correctness: A passage can be relevant and wrong.
- Chunking affects retrieval: Small chunks may lose context; large chunks may blend unrelated topics.
- Model compatibility matters: Query and document vectors must be produced in compatible spaces.
- Permissions matter: A successful similarity lookup must not bypass access controls.
- Embedding drift: Reindex when model changes invalidate stored representations.
- Multilingual and domain performance vary: Evaluate on your own material and users.
8. How to evaluate retrieval
Create queries with known relevant documents. Use metrics such as Recall@k (how often relevant content appears in the first results) and MRR (how early the first relevant result appears). Also measure latency, access-control correctness, and outcomes on real user questions.
The next step is Retrieval-Augmented Generation: retrieve relevant context and pass it to a language model so the answer can use external evidence.
Related concepts
- Vectors and Matrices
- Text Processing, Tokenization, and Embeddings
- Information Retrieval and Search
- RAG: Chunking, Indexing, and Reranking
References
Related articles in
- AI Agents and Tool-Using Workflows — Learn agent goals, tool invocation, planning, state, permissions, and how to evaluate multi-step AI workflows.
- AI Development Tools: Git, Jupyter, and APIs — Learn how Git, Jupyter notebooks, Python environments, and APIs fit together in reproducible, secure artificial intelligence workflows.
- How AI Is Changing Theoretical Computer Science — AI is beginning to help researchers explore algorithms, mathematical structures, and formal proofs. Here is what it can do, what still needs human judgment, and why verification matters.