Natural Language Processing / Tasks
Meaning-based retrieval and answering.
Reviewed by Yuvaraj
When you type a question into a search box, the naive approach is to match the words you typed against the words stored in each document. That is keyword search, and it breaks the moment a user asks about "resetting my password" while the help article says "change your credentials." Semantic search fixes this by matching meaning instead of surface text: it turns both the query and every document into numeric vectors called embeddings, then finds the documents whose vectors point in nearly the same direction. Question answering (QA) builds on top of retrieval, and Retrieval-Augmented Generation (RAG) uses the retrieved text to keep a language model's answers grounded in real sources.
Keyword methods such as TF-IDF and BM25 score a document by how many query tokens it contains, weighted by rarity. They are fast, transparent, and unbeatable on exact strings like error codes or product SKUs, but they fail on synonyms and paraphrase because "password" and "credentials" are different tokens. Semantic search compares learned representations, so paraphrases land near each other, at the cost of sometimes missing an exact keyword. Production systems often run hybrid search (BM25 + dense) to get both.
| Keyword (BM25) | Semantic (dense) | |
|---|---|---|
| Matches | Overlapping tokens | Meaning / paraphrase |
| Synonyms | Misses them | Handles them |
| Exact IDs | Excellent | Can drift |
| Needs a model | No | Yes (embeddings) |
Ask about this lesson, or about anything in AI. Answers cite the lessons they draw on.
Finished this lesson?
Mark it complete to earn XP, keep your streak, and schedule a review.
In dense retrieval you embed the query and every passage with the same model into one shared vector space, then rank passages by their similarity to the query and keep the nearest neighbors. Cosine similarity is the usual metric because it measures the angle between vectors and ignores their length. At scale you do not compare against every vector; an approximate nearest-neighbor (ANN) index (HNSW, IVF, FAISS) finds the closest ones in sub-linear time.
For a curious beginner
How it is actually used
The underlying mechanism
Take toy 3-dimensional embeddings (real models use hundreds to thousands of dimensions):
Dot products: and .
Norms: , , .
Cosine scores: and .
Because , Passage A ranks first and is the context handed to the QA step, even though it never repeats the word "reset."
Extractive QA selects a literal span from a retrieved passage, a reader model predicts start and end token positions, so the answer is always verbatim source text (great for auditability, but it cannot synthesize across passages). Generative QA feeds the passages to an LLM that writes a fluent answer in its own words, optionally citing sources. Generation is more natural and can combine evidence, but it can also hallucinate if the context is weak.
RAG is the pattern that makes generation trustworthy: retrieve top-k passages, inject them into the prompt as context, and instruct the model to answer only from that context and cite it. The retrieved text grounds the answer, shrinks hallucination, and lets you update knowledge by re-indexing documents rather than retraining the model.
Common mistakes