Originally published as a Technical Brief on dalby.md.
An embedding represents text as a list of numbers a search application can compare. Follow an account recovery question to see how a model learns to match it with useful instructions, even when the question and instructions use different words.
The idea
An embedding model turns a piece of text into a vector, an ordered list of numbers, so that a program can compare texts by meaning. Comparing vectors to find matches is called vector search. Search features, support tools and assistants that answer from documents use it to find text that shares meaning rather than words. Sentence Transformers, an open-source Python library for embedding models, documents the main ideas, and this brief links to its guides. Sentence Transformers semantic-search guide
This brief follows one example throughout. A user asks, "How do I get back into my account?" The support manual says, "Reset your password from the sign-in page." The two share almost no words, yet the manual answers the question. A passage is a section of a document. Finding the passage that answers a question is called passage retrieval. Any stored passage can be compared with any question; a passage is relevant to a question when it answers it. Finding relevant passages by meaning rather than shared words is called semantic search. All sample documents, vectors and rankings below are illustrative. Sentence Transformers semantic-search guide
Where you meet it
With semantic search, users can type their own wording instead of the vocabulary used by the documentation. The same vectors can find related support tickets or group similar feedback. They are also a common retrieval step in retrieval-augmented generation (RAG), where one part of the application retrieves text and a language model, the kind of model behind chat assistants, writes an answer from it. Original RAG paper
An embedding is a list of numbers
An embedding can represent a sentence, a paragraph or a longer passage, and some models also embed images or audio. Imagine the model produces this shortened vector, which the application stores alongside the support text:
{
"text": "Reset your password from the sign-in page.",
"vector": [0.12, -0.48, 0.73, 0.09]
}
Each position is a dimension. The password reset vector above has four dimensions, one for each number. A real model produces hundreds or thousands. E5, a family of open embedding models from Microsoft researchers, comes in three sizes whose vectors have 384, 768 and 1,024 numbers. E5 embeddings · E5 model configurations
Nobody chooses what each number means. During training the model learned to produce numbers that place texts about similar things close together. A single number has no label such as "password". The search compares all four numbers together when it checks whether the password reset passage is a useful match.
"I forgot my password" and "I cannot sign in" might produce nearby vectors despite their different wording.
The embedding model produces the representation
The model is one large calculation. Its parameters are the fixed numbers inside it, typically tens to hundreds of millions of them, set by training rather than by a person. E5's three sizes have 33, 110 and 330 million. Every text goes through the same parameters; the vector is the result for one text. Encode another text and you get another vector, while the parameters stay the same until the model is trained again. E5 model configurations
To encode a passage is to run it through the model and get its vector. An application does this in two situations, and can change the model in a third:
- Indexing prepares material for search. The application encodes the revised instructions and saves their vector in the collection, the set of stored vectors that the search looks through.
- Querying searches that collection. The application encodes the query, what the user types, whether a question or search words, and compares its vector with the saved ones to rank the results.
- Fine-tuning continues training an existing model on new examples. For instance, support examples can teach a model that a factory reset restores a device's original settings, while a restart turns it off and back on.
Indexing and querying use the trained model without changing its parameters, which is called inference. Indexing changes the collection, not the model. Only fine-tuning changes the model.
Some models expect an input prefix, a label before the text. E5 uses query: before the question and passage: before the password reset instructions. The model's documentation says how to mark a question and a document; follow it, or the vectors will not be comparable. Vectors from two different models are not comparable, even when both have 768 numbers, a common size. E5 training paper · Sentence Transformers semantic-search guide
How training connects questions with answers
Most of a model's training happens before you download it: its makers train it on large collections of examples. A training example for a support search model could pair a question about a forgotten password with a recovery passage and add a competing passage about a different problem.
| Role | Example |
|---|---|
| Question | "I forgot my password. How can I sign in?" |
| Relevant passage | "You can request a password recovery link by email." |
| Irrelevant passage | "You can turn off email alerts for new sign-ins." |
In contrastive training, the model learns by comparing a useful match with competing matches. Each comparison produces a similarity score, a number a search can use to rank a match. If the passage about alerts scores higher for this question, a training step nudges the parameters a little to raise the recovery passage's score and lower the other. Across many examples, such as the questions and upvoted answers the E5 researchers collected from Stack Exchange, the model learns which kinds of passages tend to address which kinds of questions. Sentence Transformers retrieval-training guide · E5 training paper
That is why "How do I get back into my account?" can match "Reset your password from the sign-in page" although neither sentence was in the training data. Test that with your own questions rather than assuming it. Searching for a passage that answers a question is called asymmetric semantic search, because the question and the answer play different roles. Searching for similar support tickets, where both texts play the same role, is symmetric semantic search. The Sentence Transformers guide recommends different pretrained models for the two, so choose one trained for your kind of search. Sentence Transformers semantic-search guide
Similarity turns vectors into a ranking
Once both the question and each passage have a vector, the application can calculate their similarity score. One way to do it is cosine similarity, which compares the directions of two vectors. Picture [1, 2] and [2, 4] as arrows starting at zero. The second arrow is twice as long, and both point in exactly the same direction, so their cosine similarity is 1.
Models can be trained for other comparisons, so use the one the model's documentation names. Sentence Transformers similarity measures
You do not need matrix mathematics to use this. This JavaScript sketch ranks passages that already have stored vectors. encodeQuestion and similarity stand for the application's embedding and comparison functions:
const questionVector = encodeQuestion(question);
const results = passages
.map(passage => ({
passage,
score: similarity(questionVector, passage.vector)
}))
.sort((a, b) => b.score - a.score);
The search might rank password reset above changing a billing address. The comparison is mathematical, but training makes the score useful for estimating which passage could answer the question.
A cosine similarity of 0.8 tells you how closely two vectors point in the same direction. The chance that the passage answers the question correctly is a different measurement. If you set a threshold, such as showing only results with a score of at least 0.8, test it with real questions, including ones your documents cannot answer.
A nearest neighbour is the stored vector closest to the question's vector: the one with the best similarity score under the chosen comparison rule. If your collection contains only billing documents, one of them will still be the nearest neighbour for the account recovery question. Your application needs a way to say that it found no useful match.
Semantic search and keyword search solve different problems
Lexical search, also called keyword search, finds and ranks documents by the words they share with the query. The support application might search for the user's problem or for an exact error code and firmware version. Firmware is the software that runs on the device itself, such as the software shipped with a network hub.
The two searches suit different questions:
| Query | Better with | Why |
|---|---|---|
| "I cannot get into my account" | Semantic search | The recovery procedure says "reset your password": no words in common |
| "Error AUTH-1042 on firmware 3.7" | Lexical search | A page about this error contains "AUTH-1042" and "3.7", the exact words; a semantic model might rank a similar error as high |
Lexical search can miss a paraphrase, a different way of saying the same thing, such as "I cannot get into my account" and "I'm locked out". Hybrid search combines results from both methods. The application runs the same query through both searches and gets two ranked lists: the keyword search might find the exact error code while the semantic search finds the recovery guide.
Rank fusion merges the two lists into one. It cannot simply add the scores: the keyword search might give a passage 12.4 and the semantic search 0.82, numbers on different scales. So it keeps only each result's position in each list. Reciprocal rank fusion (RRF), a common method, works like a vote. Think of each list as casting a vote for every passage it finds. First place gets a slightly stronger vote than second place. A passage that appears in both lists gets two votes.
Suppose someone searches for "How do I get back into my account?" The application runs a keyword search and a semantic search. Each search gives a passage 1 ÷ (60 + position) points, where position starts at 1. A search that doesn't return the passage gives it zero points. The constant 60 keeps the top few positions from outweighing everything else: first place gets 1/61 ≈ 0.016 points and second place 1/62, only slightly less. So a recovery guide ranked second by both searches gets 1/62 + 1/62 ≈ 0.032 points and beats a passage ranked first by one search and missing from the other, at 0.016. Qdrant hybrid queries
Reranking is an optional last step that takes the first results, such as the merged list, and puts them in a new order. For example, a second model, often called a cross-encoder, can read the question and each shortlisted passage together, instead of encoding each text separately as the embedding model does, and move the password reset steps above a passage about sign-in alerts. This adds work for each passage, so it usually runs on a shortlist.
For a first implementation, compare lexical and semantic results on the same questions. Add a hybrid or reranking stage when the missed results show what that extra stage should fix.
Prepare documents before embedding them
A long manual covers many topics, and the model can only process a certain amount of text at once. Applications divide documents into smaller passages called chunks. This step is called chunking. Each chunk gets its own embedding, so the search can return the password reset steps as a result on their own. Each stored chunk is a passage the search can return.
For the support example, keep a heading such as "Resetting an administrator password" with its steps. A paragraph containing only "Follow these instructions" carries too little context. A table row without its column headings may lose its meaning.
Document tools such as Docling, an open-source library that converts PDFs and other documents into structured text, can use headings to preserve that structure and split a section that is longer than the model accepts. Docling chunking documentation
Store enough information to open the original evidence: document identifier, section, page, revision and access permissions. When a document changes or is deleted, update or remove its chunks.
There is no universal chunk size. Test whether your chunks contain enough information to answer a question and remain focused enough to retrieve. A ten-step recovery procedure may need a different boundary from a list of independent error codes.
What a vector database stores
A vector database stores vectors and lets an application search for nearby ones: it holds the collection. Qdrant, an open-source vector database, calls each record a point: an identifier, the vector and a payload of extra data. The payload is Qdrant's term and has nothing to do with the body of an HTTP request. For the password reset passage, it could hold the text and metadata that describes where it came from: the manual's identifier, section and revision, and which customer owns it. Qdrant points documentation
For a small experiment, the application can compare the question with every stored vector. At larger scale, the database builds a vector index over the collection, a separate data structure that reaches likely matches without checking every vector. The index speeds up the search but does not change the stored vectors. This approximate nearest-neighbour search is faster, but it can miss a passage that an exhaustive comparison would return. Qdrant indexing documentation
Use filters to limit which passages the search may return. In a system that serves several customer companies, each company is a tenant. A filter on the tenant in the payload restricts the search to the manuals of the signed-in user's company, so the similarity score ranks only passages the user may see. The Qdrant component brief covers points, indexes and filters in more depth.
RAG adds answer generation after retrieval
So far, the application finds passages and shows them to the user. Adding a language model is a choice the developer makes when the application should also write an answer. In the terms of the original RAG paper, the retriever is the part that finds the text and the generator is the language model that writes an answer from it. Original RAG paper
The two models connect through text, not vectors. The application embeds the question, finds the password reset passage, and sends the original question with that passage's text to the language model. Keep the source text and a reliable route back to the document, so the answer can cite it.
Check the two parts separately: first whether retrieval found the right procedure, then whether the answer follows it and cites the right section. A fluent answer can still omit a required step, and no generator can recover evidence that never reached it. RAG also works with other retrieval, such as keyword search or database queries. The brief on Docling and the LLM wiki asks whether another approach can replace RAG.
Fine-tuning changes what counts as a good match
Start with a pretrained model, one whose original training is complete, and test it against your own material. Fine-tuning becomes relevant when the model repeatedly confuses terms that your examples can help it tell apart, such as an ordinary restart and a factory reset. Each training example then pairs a question, such as "How do I erase the hub before selling it?", with the passage that answers it and a competing passage about the other procedure. Review the examples: two different passages can both answer a question, and marking one as wrong teaches the wrong distinction. Sentence Transformers training overview
After fine-tuning, rebuild the stored vectors with the new model before comparing them with new query vectors. The companion brief, Understanding EmbeddingGemma 2: from search to fine-tuning, shows a model being adapted, and one that also compares images, audio and video.
What to try first
Before adding hybrid search, reranking, RAG or fine-tuning, measure the simplest version. Create a set of questions with known relevant sources. Include exact identifiers, paraphrases, ambiguous wording and questions without an answer. Keep the final test questions out of training. Keep related documents together when deciding which material goes into training and which goes into testing. That keeps two nearly identical versions of a password reset guide from landing on opposite sides.
Retrieval metrics are numbers that tell you how well search results match your test questions. Each of these measures a different aspect of the results. Sentence Transformers retrieval evaluator
-
Recall@k measures the fraction of all relevant passages that appear in the first
kresults. If five passages are relevant and the first ten results contain four, recall@10 is 4/5. - Mean reciprocal rank (MRR) measures how early users see their first useful result. If the first relevant passage is third, its reciprocal rank is 1/3. MRR averages that value across the test questions.
- Normalised discounted cumulative gain (nDCG) rewards putting the most useful passages near the top: a useful passage counts for less the further down it appears. If you rate the exact password reset steps as highly relevant and a general sign-in guide as partly relevant, nDCG gives a better score when the steps come first. It scores each order against the best possible one, so 1.0 means the most useful passages came first.
A first experiment can be small. The Sentence Transformers semantic-search guide includes a short Python example that encodes a small set of sentences and ranks them for a few questions; run your test questions through that and through a keyword search, and compare the metrics.
Record latency, the time from sending a question to receiving the results, and memory use alongside relevance. A small support search page can return passages and source links while you measure these. Once that retrieval works, the same evidence can support a RAG answer. Fine-tuning should address a measured weakness in that feature.
AI Disclosure
Researched and drafted with AI assistance, then reviewed by Søren Dalby. Sources are linked; errors may remain, and corrections are welcome.
Sources
- Sentence Transformers: semantic textual similarity
- Sentence Transformers: semantic search
- Sentence Transformers: training for passage retrieval
- Wang et al.: E5 training data
- Wang et al.: E5 embeddings from average pooling
- Wang et al.: E5 model configurations
- Docling: chunking
- Qdrant: points
- Qdrant: indexing
- Qdrant: hybrid queries
- Lewis et al.: Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks
- Sentence Transformers: training overview
- Sentence Transformers: evaluation



Top comments (2)
The unanswerable questions are a useful addition to the test set. I'd also keep a small set where an old and a current procedure use almost the same wording. Check that retrieval returns the current revision and that the answer cites it; relevance alone can look good while the user gets outdated steps.
the account recovery example made the vector thing click for me, "get back into my account" vs "reset your password" is a nice pick.