Language ModelsExplainer

What Is a Vector Database? Keyword Search vs Meaning Search on 40 Questions

A vector database stores text as lists of numbers and finds the entries closest in meaning to a question. I ran the same questions through keyword search and meaning search on my laptop. When people used their own words, keyword search found the right document in the top three 9 times out of 20, meaning search 20 times. On bare error codes, where I expected meaning search to fail, it did not.

By Vladimir Beskorovainyi

8 min read

Illustration: BESK TECH

A vector database is a database that stores data as vectors, lists of numbers produced by an embedding model, and retrieves the stored items whose vectors are closest to the vector of a query. Because texts with similar meaning get similar vectors, it finds relevant documents even when the question uses different words. It is a common retrieval layer for semantic search and RAG systems. It can be a dedicated product or an extension of an ordinary database, such as pgvector for PostgreSQL.

A vector database stores text as vectors, lists of numbers that encode meaning, and answers a query by returning the stored items whose vectors are closest to the query's vector. The useful questions are when it beats ordinary keyword search, when it does not, and whether you need a separate database for it at all. I measured all three on my laptop. For the first two I used 60 documents and 60 questions, and no database at all, which is itself part of the answer.

What a vector database is

An embedding model turns a piece of text into a vector of fixed length. EmbeddingGemma, the model I used, produces 768 numbers per text. It is trained so that texts with similar meaning land close to each other. "My refund has not arrived" and "the money for the returned item is still missing" share almost no words, but their vectors are near each other. I covered how embeddings behave in What Is an Embedding.

A vector database keeps those vectors next to the original text and does one job well: given a new vector, find the nearest stored ones fast. Search then works in three steps.

  1. Every document is converted into a vector once, when it is added.
  2. The question is converted into a vector by the same model.
  3. The database returns the documents whose vectors are closest, usually by cosine similarity.

Keyword search, by contrast, counts which words of the question appear in each document and how rare those words are. The standard formula is BM25. It is fast, predictable and blind to synonyms. A large language model does neither of these jobs by itself; it answers from whatever the search hands it.

What I measured

I wrote a small knowledge base for an imaginary online shop: 60 short documents about refunds, delivery, payments, accounts, 20 error codes and 8 subscription plans. Then 40 questions, each with one correct document, in two groups of 20, and later a third group.

  • Meaning questions use other words than the document. "The vase came smashed into pieces" should find the document about items that arrive broken.
  • Exact questions hinge on a code or a name. "I got ERR-4017 at checkout" should find ERR-4017 and not ERR-4071, ERR-4107 or ERR-4701, which exist next to it on purpose.

I wrote the method, the documents and the questions before the first run. Three ways of searching ran on the same data:

  • BM25 keyword search (the rank_bm25 library, default settings, lowercase words, no stemming, no stop-word list);
  • vector search with EmbeddingGemma (308M parameters, embeddinggemma in Ollama 0.40.4), running locally, with the query and document prefixes from its model card;
  • a hybrid that merges both rankings with Reciprocal Rank Fusion: each document gets 1/(60 + its place) from each list, and the sums decide the order.

The machine was a MacBook Air M4 with 24 GB of memory. The measure is whether the correct document lands first, and whether it lands in the top three.

Results

Questions (20 each)Keyword, top 1 / top 3Vector, top 1 / top 3Hybrid, top 1 / top 3
Meaning: other words4 / 917 / 2011 / 14
Exact: codes and plan names18 / 2019 / 2019 / 20
All 4022 / 2936 / 4030 / 34

Embedding all 60 documents took about one second. A single query, including turning the question into a vector, took 15 to 20 milliseconds.

Where keyword search loses

On meaning questions keyword search put the right document in the top three only 9 times out of 20. "The vase came smashed into pieces" ranked the right document 49th of 60, because the document says "broken", not "smashed". "Do I owe customs fees if I order from abroad" ranked it 42nd, because the document says "import duties". When the reader and the writer of a document use different words, keyword search has nothing to hold on to.

Vector search found every one of those 20 in the top three and 17 in first place. It missed first place three times, and each time the right document was second. For "my headphones stopped working after a year, will you fix them for free" it put the repair document first and the warranty document second. In my view both are reasonable answers.

Where I expected vector search to lose

The common advice is that vectors are weak on exact tokens: product codes, invoice numbers, names. I built the error codes to test exactly that, with four codes made of the same digits in different order.

On this set the expectation did not hold. Vector search put the right document first in 19 of 20 exact questions. The single miss was the kind the advice warns about: ERR-8152 was confused with ERR-8512, same digits, both about invoice details. Keyword search failed to put the right document first twice: on "Pro 12 benefits", where "Pro 120" ranked first, and on "I got ERR-4017 at checkout". Both times the right document was second.

After the run I noticed a weakness in my own questions. Many exact questions also contain a topic word, such as "with my gift card" or "when saving company details", and that word could be helping the vector model. So I wrote 20 more questions that are nothing but the code or the plan name: "ERR-4071", "Pro 24". On those, vector search put the right document first 20 times out of 20. So did keyword search. The one miss in the main set happened because of the extra words, not despite them: "when saving company details" pulled the model towards the neighbouring document about company names on invoices.

The hybrid was not a free improvement

Combining both rankings is a common recommendation. Here the equal-weight hybrid scored worse than vectors alone: 14 of 20 in the top three on meaning questions against 20. When keyword search ranks the right document 49th or 42nd, it drags the combined place down. I then gave keyword search a weight of 0.3, chosen before the run and not tuned. That recovered most of the loss, 18 of 20, but still did not beat vectors alone.

The reason is simple. A hybrid helps when each method fails on different questions. Here vector search never left the right document out of the top three, so keyword search had nothing to rescue. The only question where keyword search beat vectors on first place, ERR-8152, stayed second in both hybrids. On your data the picture may differ, which is the point: test the hybrid on your own questions before you add it.

Do you need a separate vector database?

Not always. The search itself is simple arithmetic. For 60 documents I did not use a database at all: the vectors fit in memory and I compared the query with each of them directly. To see how far that goes, I timed the same direct comparison on random vectors of the same length. One query took 0.7 ms against 10,000 vectors, 4.8 ms against 100,000 and 49 ms against a million, which needed 3 GB of memory. Time grows in proportion to the number of vectors. On one laptop, a million documents is still searchable without any index.

A database starts to matter for three reasons:

  1. Scale. Past a few million vectors, or with many users querying at once, comparing against each one becomes too slow and the memory adds up. Vector databases build approximate indexes. A widely used one is HNSW, a layered graph that jumps quickly towards the nearest neighbours. They trade a small chance of missing the exact nearest item for a large gain in speed.
  2. Updates. Documents change, and vectors must be added, replaced and deleted together with the text.
  3. Filters. Real queries combine meaning with conditions: only this customer's documents, only this year, only published ones.

If your data already lives in PostgreSQL, the pgvector extension adds a vector column type and nearest-neighbour search to the same database. That keeps vectors, text and filters in one place, under one backup and one access policy. A dedicated vector database makes sense when vector search is the main workload and the volume is large.

Vector databases and RAG

In a RAG system, retrieval-augmented generation, the model answers from documents the system found first. The vector database is that finding step, and the model cannot use a document it never received. In my test, keyword search left the right document out of the top three on 11 of 20 everyday questions. A RAG system built on it would have answered those 11 without the right material. I measured a case where retrieval added nothing in What Is RAG. The test in this article is the other half: how often retrieval finds the right document at all.

What to take from this

  1. Vector search is a sound default when users describe a problem in their own words. On my questions it found the right document in the top three 20 times out of 20, against 9 for keyword search.
  2. Do not assume vectors fail on codes, and do not assume they succeed. Test with your own codes, including near-identical ones, and with the short queries your users actually type.
  3. Do not add a hybrid by reflex. Measure it. In my test the equal-weight version made results worse, and the weighted one only came close to vectors alone.
  4. Start inside the database you already have. A million vectors fit on a laptop. Move to a dedicated vector database when volume, update rate or concurrent load requires it.

The test is small: 60 documents, 60 questions, one model, no index. It shows the mechanism, not a ranking of products. The method that matters is the same at any scale: write the questions and the correct answers before you run anything, and count.

Sources

  1. EmbeddingGemma model card, Google
  2. pgvector: open-source vector similarity search for Postgres
  3. Cormack, Clarke, Büttcher. Reciprocal Rank Fusion outperforms Condorcet and individual Rank Learning Methods, SIGIR 2009
  4. What Is RAG? A 1,534-Question Test Where Retrieval Only Took Things Away, BESK TECH

Vladimir Beskorovainyi

Vladimir Beskorovainyi, Enterprise AI Architect & CTO, author of the Amazon bestseller "Enterprise AI: The KATA Way", founder of Besk Tech (besk.tech)

More in this section

Get new articles by email

One message when something new is published. No other mail, and you can stop at any time.

The address is used for this one letter and nothing else. Privacy

← All articles