Lexical Search for Exact Terms
In this article
- Trace a lexical lookup from a question's terms to the chunk ids that hold them.
- Explain why a vector search can rank a look-alike chunk above the one that holds an exact code.
- Spot the parts of a question that must appear as written, and say which index should handle them.
- Say when a question in the customer's own words needs only the vector side.
- Terms defined here: lexical search, hybrid retrieval.
Overview
A customer types the error code from their router's screen and asks what it means. Your search finds the right kind of page, an error table from a router manual, but from the wrong router, and it is confident about it. Nothing is broken: a search that compares meanings did exactly what it does, and the two manuals mean almost the same thing.
The fix is a different kind of search, one that looks for the code itself. Retrieval Methods for RAG gave the rule; here you trace how the lexical index does it. You see how it finds a chunk by an exact term and why a meaning-based search misses it. By the end you can look at any question and say which part of it needs the exact match and which part does not.
A question with a code in it
Brightline, a made-up home-internet provider, runs a support assistant. Its search code holds about 2,000 chunks, short stored pieces of its help pages and router manuals, and puts the best 3 into each request to the model, the top 3. Each chunk has an embedding, a list of numbers that stands for its meaning, made with OpenAI's text-embedding-3-small and stored in PostgreSQL with pgvector.
A customer asks Q1: "What does E-42 mean on the BL-R7?" Vector search turns the question into a vector and compares it with the stored chunk vectors by a similarity measure such as cosine similarity, where a higher number means closer. [2] Brightline embeds the question with the same model as the chunks and keeps the closest ones. Brightline's manuals include two error tables: manual-bl-r7#e42, which says "Error E-42: the BL-R7 cannot reach the network", and manual-bl-r5#errors, the error table of the older BL-R5 router, which reads almost the same and has no E-42 row. Before you look below, predict: which of the two comes first?
The vector search returns this top 3 (made-up scores):
| rank | chunk id | cosine similarity |
|---|---|---|
| 1 | manual-bl-r5#errors | 0.61 |
| 2 | manual-bl-r7#e42 | 0.60 |
| 3 | help-router-setup#1 | 0.52 |
The wrong router's table wins by 0.01. The right chunk did make the top 3, so the model may still answer well, but nothing in these numbers tells you which chunk holds "E-42". A gap of 0.01 is noise: add one more look-alike manual and the right chunk could fall out of the top 3.
Why the wrong manual wins
Ask what the question's vector has to go on. Its meaning is roughly "an error code on a Brightline router, what does it say", and both tables are exactly that: error codes, router names, a short line of meaning each. In this made-up pair, the code "E-42" and the model name "BL-R7" are a few characters in a sentence full of ordinary words, and everything else in the two tables is the same kind of text. The BL-R5 table is, as far as meaning goes, a near twin.
Search vendors describe the same gap. Pinecone writes that keyword search matches specific terms such as product codes, error strings and names, dense vector search matches on meaning, and each misses what the other catches. [1] Microsoft's RAG guidance recommends full-text search on keyword and entity fields for content whose meaning is similar while the entities or keywords differ, which is the BL-R5 and BL-R7 tables exactly. [2] It also notes that cosine-style similarity captures general relatedness and does not judge whether a chunk answers the question. [2] LlamaIndex puts it bluntly: embeddings can fail to return chunks that contain the matching keywords. [3] One way to picture the toy: a dense vector is one list of numbers for the whole chunk, the two tables' lists come out nearly the same, and the one code that differs barely moves the score.
So 0.61 says "close in meaning". It never says "contains E-42".
What a lexical index stores
A lexical search, also sold as keyword search or full-text search, takes the opposite approach: it ignores meaning and looks up the words themselves. [1,2] Its index stores each term with the ids of the chunks that contain it, much like the index at the back of a book, and a query looks its own terms up there. [4] Brightline builds one over the same 2,000 chunks, with the same chunk_id values the vector index uses.
Before anything is stored, the text goes through the analyzer, the step that lowercases text, splits it into terms and drops filler words such as "the", and the same step runs on the question; it decides what counts as a term. [4] Brightline's analyzer also keeps hyphenated codes whole, so "BL-R7" and "E-42" each become one term; an analyzer that split at the hyphen would leave only common pieces such as 42.
Here are five entries of Brightline's lexical index (made up, lists shortened with "…"):
| term | chunk ids that contain it |
|---|---|
bl-r5 | manual-bl-r5#errors, … |
bl-r7 | manual-bl-r7#e42, manual-bl-r7#reset, … |
e-42 | manual-bl-r7#e42 |
error | manual-bl-r7#e42, manual-bl-r5#errors, … |
peak | help-busy-hours#1, … |
The lookup is short. This sketch is pseudo-code, not run:
hits = {}
for term in analyze(query):
for chunk_id in index.get(term, []):
hits.setdefault(chunk_id, set()).add(term)
return hitsTrace Q1 through it. The analyzer turns the question into terms including e-42 and bl-r7. The e-42 entry holds one id, manual-bl-r7#e42, and that id is also in the bl-r7 list, so it is the only chunk holding both. manual-bl-r5#errors sits under neither e-42 nor bl-r7, so the lookup never returns it; it would appear only under a term the question does not contain, such as error. Common terms like mean point to many chunks and say little. The exact code points at exactly one chunk, and it is the right one.
Words that must appear as written
Some parts of a question are constraints, not hints. An error code such as "E-42", a model name such as "BL-R7", and wording a customer pastes in quotes from a screen, such as "cannot reach the network", must appear in the chunk as written, or the chunk is about something else. A lexical index can test that directly. Microsoft's guidance notes that full-text search can be set to match when any of the terms appear or only when all of them do. [2] PostgreSQL's query language has an AND operator that needs both terms present and a FOLLOWED BY operator, <->, that matches only when words sit next to each other in the given order, which is how a quoted phrase is checked. [4]
Some constraints never need the text. If the customer picked their router from a menu, the application already knows the model, and a metadata filter on a stored field can narrow the candidates before ranking, so other routers' manuals never compete. [1] Lexical search is for constraints that arrive only as typed words.
When the words do not match
Lexical search has the mirror-image blind spot. A customer writes Q2, "My Wi-Fi gets slow every evening". The chunk that answers it, help-busy-hours#1, says "Speeds can drop at peak times": no "slow", no "evening", no "Wi-Fi". Look at the table again: the peak entry has that chunk, but no term of the question leads to it, so the lexical index cannot find it at all, while the vector search ranks it first because the meanings match. A question in the customer's own words, with no code or name in it, is the vector side's job. [1]
Two retrievers over the same chunks
So lexical search is a first-class retriever for Brightline, not a fallback. Both indexes cover the same chunks with the same ids, and the search code, not the model, runs both for every question. One chunk set matters: if the lexical side indexed an older edition of the manuals, the same chunk id would point at different text on each side.
Running a keyword search and a meaning-based search for the same question and combining their results is called hybrid search, and LlamaIndex names it as the usual answer when embeddings miss matching keywords. [1,3] How to combine two lists whose scores mean different things comes later in the topic.
The rule to carry into a design review: when part of a query must appear as written, a code, a model name or quoted wording, give that part to a lexical index, and never read a vector score as proof that the term matched. A question such as "My BL-R7 shows E-42 and drops every evening" needs both: the code and model name for the lexical side, the evening complaint for the vector side.
Common mistakes
- Trusting the top vector hit on a code query. The symptom is a confident answer from the wrong manual, such as the BL-R5 table for a BL-R7 error. Check that the code appears in the chunk, or send the code to the lexical index.
- Testing only paraphrased questions. A test set of questions in everyday words makes vector search look complete. Add questions with codes and model names, and blame the embedding model only after a lexical lookup also fails.
Sources
- Hybrid search overview (Pinecone docs). Pinecone. Checked 29 Oct 2026.
- Develop a RAG Solution on Azure - Information-Retrieval Phase (Azure Architecture Center). Microsoft. Checked 21 Nov 2026.
- LlamaIndex docs: Basic Strategies. LlamaIndex. Checked 24 Oct 2026.
- Full Text Search: Introduction (PostgreSQL documentation). PostgreSQL Global Development Group. Checked 24 Oct 2026.
- pgvector: open-source vector similarity search for Postgres (README). pgvector project. Checked 25 Oct 2026.
Check yourself
Check yourself
Question 1 of 13
A tool must find chunks that hold a pasted error string as written. What does that?
Your first check counts towards your result.