Prompt
nearest neighbor search embeddings
Latest observation
Oct 10, 2026
OpenAI APIWeb search: off
Nearest neighbor search over embeddings means: given a query vector, find the most similar vectors in a database of vectors.
Core idea
- Convert items into embeddings (dense vectors).
- Store those vectors in an index.
- For a query embedding, compute similarity to all or many vectors.
- Return the top-k nearest ones.
Common similarity metrics
- Cosine similarity: compares angle; common for text embeddings.
- Dot product: often used when vectors are normalized.
- Euclidean distance (L2): geometric distance.
If embeddings are normalized, cosine similarity and dot product are closely related.
Exact vs approximate search
Exact nearest neighbor
- Compares against every vector.
- Simple but slow for large datasets.
Approximate nearest neighbor (ANN)
- Trades a tiny bit of accuracy for huge speed gains.
- Used for large-scale search systems.
Common ANN methods:
- HNSW: graph-based, very popular.
- FAISS: library with many indexing options.
- Annoy: tree-based, good for read-heavy workloads.
- ScaNN: optimized for similarity search.
Typical workflow
- Embed documents/items.
- Normalize vectors if using cosine similarity.
- Build an ANN index.
- Embed the query.
- Search top-k nearest vectors.
- Optionally rerank results with a more precise model.
Python example with FAISS
import faiss
import numpy as np
# Example embeddings: 1000 vectors of dimension 128
xb = np.random.rand(1000, 128).astype('float32')
xq = np.random.rand(1, 128).astype('float32')
# If using cosine similarity, normalize vectors first
faiss.normalize_L2(xb)
faiss.normalize_L2(xq)
index = faiss.IndexFlatIP(128) # inner product for cosine on normalized vectors
index.add(xb)
k = 5
scores, ids = index.search(xq, k)
print(ids)
print(scores)
When to use it
- Semantic search
- Recommendation systems
- Duplicate detection
- Retrieval-Augmented Generation (RAG)
- Clustering / similarity lookup
If you want, I can also show:
- how to do this in Python with scikit-learn / FAISS / Annoy
- how cosine similarity works mathematically
- how to choose the best ANN index for your dataset