A search for "car" should find the article that says "vehicle". Keyword search cannot do it, and the reason is one number: zero. Without synonym expansion, BM25 gives a sentence that never contains "car" a score of exactly zero, because the term frequency is zero and no ranking constant can fix that.
In our toy lab over 42 hand-labeled sentences, exact keyword search returned 4 sentences, 2 relevant: recall at 10 of 14.3%, precision at 10 of 20%, and 0 of the 12 road-car sentences that never say "car". Matching the letters c-a-r instead returned 10 sentences, 8 wrong.
How shared contexts become vectors
An embedding turns each word into numbers learned from the words it appears with. The lab builds a toy version from 124 training sentences, using PPMI: each word gets 304 numbers, one per vocabulary word.
"Car" and "vehicle" never share a training sentence, so their direct count is zero. They are still similar because they share contexts: engine, brakes, highway, insurance, road. Their rows overlap in 16 positions, and the dot product sums to cos(car, vehicle) = 0.223. Cardboard shares none of car's contexts, so cos(car, cardboard) = 0. Scored this way, the toy embedding puts 8 relevant sentences in its top 10, 6 never saying "car": recall at 10 of 57.1%, precision at 10 of 80%.
Where it fails
The number one result was wrong. "The last train car was full of tired passengers" scored cos 0.536, ahead of both real road-car sentences at 0.496 and 0.483. It contains the query word itself, worth 1.000, and its neighbours "train" and "passengers" also point toward "car". A sum of word vectors has no word order, so "train car" and "car train" are the same vector, and one vector per spelling blends the rail and road senses of "car" together.
At a million vectors
A flat scan costs O(N times d) per query. For 1 million chunks at 384 dimensions in float32, the index is 1.536 GB and a flat scan is 384 million multiply-adds, about 31 ms at 50 GB/s of memory bandwidth.
Approximate indexes trade a little recall for far fewer comparisons. HNSW with M = 16 adds about 132 MB of graph and needs about 2,000 distance computations per query, about 500 times fewer than the flat scan. IVF with 1,000 lists and 10 probed lists needs about 11,000 comparisons, one 91st of the flat scan. Product quantization shrinks each vector from 1,536 bytes to 48.
Hybrid search wins
Running both retrievers and fusing with reciprocal rank fusion uses only ranks. With k = 60, the road-car sentence moves to rank 1. The fused top 10 still holds the same 8 relevant and 2 wrong lines, because both retrievers like the train car.
Our take
Keyword search is exact and blind to meaning. Embeddings see meaning and bring their own mistakes, starting with the train car. Run both and fuse the ranks.
Read the full article for the worked arithmetic, the PPMI recipe and the code: https://systemdesignsimple.com/why-search-for-car-finds-vehicle/
Top comments (0)