Sub topic 14
Common pitfalls: normalization, query and document prefixes, model changes, domain mismatch
This chapter assumes you can run the earlier search code: you know how to embed text with a pretrained model, compute cosine similarity, and rank documents by score. The four pitfalls below break a working pipeline without raising an error. The code runs, the scores look plausible, and the results are quietly worse.
Pitfall 1: Inconsistent normalization
Cosine similarity ignores vector length. A dot product does not. The earlier search code used doc_emb @ q and called it cosine similarity, which is only true because both sides were encoded with normalize_embeddings=True.
If you normalize the documents but not the query, the ranking for a single query is still correct, because the query length scales every score by the same factor. The scores are no longer cosine values, though, so a fixed threshold such as "keep results above 0.5" stops meaning what you think. If you mix normalized and unnormalized document vectors, the ranking itself is wrong, because longer vectors win.
import numpy as np
def cosine_similarity(a, b):
return float(np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b)))
q = np.array([1.0, 0.0])
near = np.array([0.9, 0.1]) # similar direction, short
far = np.array([3.0, 4.0]) # different direction, long
print(float(q @ near), float(q @ far)) # 0.9 3.0 (dot product prefers the long vector)
print(round(cosine_similarity(q, near), 3), round(cosine_similarity(q, far), 3)) # 0.994 0.6Choose one rule and apply it everywhere: normalize at encode time, or use a cosine-aware index. Also check what the index expects. IndexFlatIP computes inner product, so it needs unit vectors to behave like cosine.
Pitfall 2: Missing query and document prefixes
Some models are trained with a text prefix that tells them the role of the input. The E5 family expects query: before a search query and passage: before a document. Other models ship a recommended query instruction instead. If you omit the prefix, the model still returns vectors, but they sit slightly off from where training placed them, and retrieval quality drops.
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("intfloat/e5-small-v2")
docs = ["passage: Steps to recover access to your account"]
query = "query: How do I reset my password?"
doc_emb = model.encode(docs, normalize_embeddings=True)
q_emb = model.encode(query, normalize_embeddings=True)
print(float(doc_emb[0] @ q_emb))Read the model card for every model you adopt. Models such as all-MiniLM-L6-v2 need no prefix, so adding one there is also a mistake.
Pitfall 3: Changing models without re-embedding
Vectors from two different models are not comparable, even when both have 384 dimensions. Each model defines its own space. Matching dimensions only means the code will not crash.
The same applies to a new version of the same model, a different prefix, or a change in normalization. Store the model name and settings next to your vectors, and re-embed the whole collection when any of them changes.
Pitfall 4: Domain mismatch
General models learn from general text. In a specialized field such as law, medicine, or your company's internal jargon, they may treat distinct terms as similar or fail to connect synonyms they never saw. Product codes, error identifiers, and rare names are also weak spots, since the model has no meaning to attach to them.
Check for this before changing anything. Collect real queries with known relevant documents and measure recall@k. If the numbers are low, try in this order:
- Try another pretrained model that is closer to your domain.
- Add keyword search and fuse the rankings, as described in the hybrid search chapter.
- Fine-tune a model on pairs of queries and relevant documents from your domain.
Quick checklist
| Check | Symptom if wrong |
|---|---|
| Same normalization for queries and documents | Scores not in the range -1 to 1, odd ranking |
| Correct prefixes for the model | Mediocre results with no error |
| One model per index | Nonsense neighbors |
| Labeled queries from your domain | Quality problems go unnoticed |
Exercise
You switch from one 384-dimension model to another 384-dimension model and embed only new documents, leaving the old vectors in the same index. Searches run without errors but the results look random. Why, and what should you do?
Show answer
The two models define different vector spaces, so scores between a new query vector and old document vectors, or between old and new documents, carry no meaning. The matching dimension only prevents a crash. Re-embed every document with the new model into a fresh index, check recall@k on labeled queries, and then switch over.