Kolkata ·  
AI engineering
retrieval
deep · 5 min

Semantic search

Search by meaning: embed the question, then return the stored chunks whose vectors sit closest to it.

updated 27 Sept 2026

Let's start with something small enough to see all of. Nine sentences: three about machine learning, three about space, three about climate. And one question: "How do artificial neural networks work?"

We turn every sentence into an embedding with a small open model, all-MiniLM-L6-v2, which gives 384 numbers per text. We embed the question with the same model. Then we score the question against each sentence with cosine similarity and sort.

This example comes from the Sentence Transformers docs, which print the top five. We ran it ourselves on 2026-09-27 (Transformers.js, same model) and got the same five scores to four decimal places, plus the other four:

The neural-networks sentence comes first, which isn't surprising, since it shares the words. But "deep learning" comes second, and the question never says "deep" or "learning". Then there's a cliff down to Mars and carbon capture. That's semantic search: finding text by meaning instead of by matching words. Keep the top few and you have what an LLM needs to answer.

How it's built

The work splits in two. Once per document, ahead of time, we cut it into pieces small enough to embed well (chunking), embed each one, and store the vectors next to the text. Then on every question, we embed the question, score it against the stored vectors, and keep the top k.

The question has to go through the same model as the documents. Each embedding model builds its own space, and a vector from one can't be compared with vectors from another.

So each question costs one embedding call plus the comparison. If the vectors are normalized to length 1, a plain dot product ranks the same as cosine and does less work.

What meaning buys us

Keyword search like BM25 only finds documents that contain the words typed. Search by meaning can also handle synonyms, abbreviations and misspellings, because similar meanings land close together even when the letters differ.

We tried that in the same run. "neural net" put the neural-networks sentence first at 0.552. The misspelled "nueral netwroks" still put it first, but at 0.222, barely ahead of deep learning at 0.201. So typos don't break it, but they do weaken the signal a lot.

On real search tasks the gain can be large. A 2020 system called Dense Passage Retrieval, two encoders trained on a modest set of question and passage pairs, found the right passage in its top 20 between 9 and 19 points more often than a strong BM25 system.

Short questions, long answers

Our nine sentences make it look a bit too easy, since every entry is one short sentence like the question. Real search comes in two shapes.

In symmetric search the query and the entries look alike. Finding duplicate questions is the classic case: "How to learn Python online?" should find "How to learn Python on the web?". You could swap them and nothing would change.

In asymmetric search a short question has to find a longer passage that answers it. "What is Python" should find a paragraph about the language. That's the RAG case.

Models are trained for one shape or the other, so pick one trained for yours. Some retrieval models also want to know which side a text is on, and put a different prompt in front of queries and documents. Libraries give you separate calls for each (encode_query and encode_document in Sentence Transformers). For a model trained without those prompts, both calls return the same vectors, so using them costs nothing.

Checking everything, until we can't

The simplest version scores the question against every stored vector. It sounds slow, but it's just multiplication, and it's exact: we always get the true top k. The Sentence Transformers docs put the comfortable limit at about a million entries.

Past that, we switch to an approximate index that groups similar vectors, so a search only looks at a small part of the collection. Results come back in milliseconds over millions of vectors, but some close matches can be missed. How that works, and what it gives up, is vector indexes.

Where it goes wrong

Scores don't have a fixed meaning. In our run, the best match for the neural-networks question scored 0.593. The best match for "What technology is used for modern space exploration?" (the Mars rovers sentence) scored only 0.375, though it's a perfectly good answer. One cut-off score for every question would drop good results for some and keep bad ones for others.

Exact strings slip through. A query like "Error code TS-999" needs that exact string, and an embedding model may hand back chunks about error codes in general and miss the one that mentions TS-999. Keyword search catches those, so the common fix is to run both and merge (hybrid search).

It may not travel well, either. DPR's win was measured on the kind of data it was trained on. When the BEIR benchmark tested retrieval models on 18 datasets they hadn't seen, plain BM25 held up as a strong baseline, and dense models often fell behind. Those were 2020 and 2021 models, so the gap may look different now. Measure on your own data before assuming embeddings beat keywords.

And the first ranking doesn't have to be the last. For harder searches, a second model re-scores the top candidates (reranking).

One more, and it's the one that matters most for RAG. Ask our nine sentences "How can we address climate change challenges?" The global warming sentence comes first, fine. But ask for the top five, and the fourth and fifth are the machine-learning sentences, at 0.042 and 0.041, scores that mean "nothing to do with it". We asked for five, so we got five. Search always returns its nearest neighbours, even when none of them are near. Noticing that, and saying so, is saying "I don't know". Repeated questions can skip the search entirely, which is semantic caching.

Further reading

compare with