Agents

Embeddings vs keyword search — which one to use in your project

Illustration: two lenses over one stack of documents — one catching meaning, one catching the exact word

The usual pitch for semantic search: old word-matching is dumb, new meaning-matching is smart, take the new one. Half of that is true. But here's what shows up in practice: semantic search fails at exactly what keyword search nails — a product SKU, an error code, a surname, a contract number. Ask for «error E-4302» and you'll get five errors that feel related, but not that one. Let's sort out what works where, and what to put in your project.

How each one works

Keyword search (also called lexical or full-text) matches words. It reduces words to their base form, drops stop words, and scores on how rare a word is and how often it appears in a document. The classic algorithm is BM25 — it powers full-text search in Postgres and Elasticsearch.

Embedding search (semantic, vector) compares meanings instead of words. Every document is turned into an embedding — a set of numbers — ahead of time. The query becomes the same kind of set, and the system finds the closest ones by distance. The words need not overlap at all: «how do I get my money back» finds a doc titled «refund policy». More on that in the semantic search piece.

Criteria, side by side

Synonyms and rephrasing. Embeddings win: they match on meaning, wording doesn't have to line up. Keyword search misses whenever the text uses different words.

Exact matches (SKUs, codes, names, IDs). Keyword search is perfect: SKU-88213 lands precisely. Embeddings are weak here — a rare code barely moves the vector, so you get back «something similar».

Typos. Embeddings shrug them off. Full-text search without fuzzy matching misses.

Speed and cost. Keyword: the index builds instantly and a query is effectively free. Embeddings: you must run every document through a model once (that's money), store vectors, and recompute when text changes.

Barrier to entry. Keyword search ships with your database: in Postgres it's to_tsvector and a couple of lines of SQL. Vectors need either an extension like pgvector or a separate store.

Explainability. With keywords you can see why a document matched — that word. With embeddings there's no explanation: «the vector was closer». Debugging is noticeably harder.

New words. A product name coined yesterday is findable by full-text search immediately. The embedding model has never seen it — to the model it's a meaningless string.

Why production almost always runs both

Hence the conclusion the industry landed on: hybrid search. The query hits both engines in parallel and the results merge into one list.

It works like this: full-text brings back exact matches, vector search brings back close meanings, then their ranks are combined by a shared formula into a single top list. When one method misses, the other covers.

This matters most for RAG: pure vector search is why a bot so often says «I couldn't find it» about a specific order number that is, in fact, sitting in the database.

Who should use what — bluntly

Keyword only, if you're searching a product catalogue, documents with numbers, logs, code, or tables with IDs. Also if you have a few thousand records and no appetite for extra infrastructure. It's cheaper, faster and more predictable.

Embeddings only, if you have prose and real user questions: a knowledge base, support, articles, transcripts. There the question's wording almost never matches the answer's wording.

Hybrid, if you're building documentation search or an AI assistant over your own data — which covers most real projects. Start with full-text (it gives you a working result in an evening), then add the vector layer and merge the results. The reverse order — vectors first, then «why can't it find the SKU» — is the most common beginner mistake.

Do I need a dedicated vector database?

Not at first. Up to hundreds of thousands of documents, pgvector inside ordinary Postgres is plenty — and you get hybrid search without a second store. A dedicated database earns its place at large scale and high load.

How much does embedding my documents cost?

Embedding models are far cheaper than generative ones, and you pay once per document. Costs show up when your texts change often and everything must be recomputed.

Does a full-text index help embeddings?

Not directly — they're independent mechanisms. But in a hybrid setup they complement each other: exact hits from one, meaning from the other.

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →