Back to blog
RAGAI EngineeringSemantic Search

The Search Before the Answer

15 October 2026 | 5 min read | Updated 28 October 2026

How AI finds useful passages in your documents, and why the closest match may not be enough.

Imagine asking a shop's AI assistant:

I bought X200 headphones last week. They've stopped working. Can I bring them back?

The assistant needs the shop's policy to answer. It can search the shop's documents, find useful passages and give those passages to AI alongside the question. AI then uses that material to draft a reply.

This approach is called retrieval-augmented generation, usually shortened to RAG. We will follow the question from the shop's handbook to an answer, including a search result that looks relevant but concerns the wrong situation.

A question about returning headphones leads to a search of the shop policy. AI receives the retrieved passages with the question to draft an answer.
RAG finds passages to give AI alongside the question.

Keep the rule and its conditions together

Before questions arrive, the software behind the assistant prepares the handbook for search. It gets readable text from the documents; scanned pages may need text recognition. It then divides the text into searchable passages, called chunks.

One passage says:

Opened headphones may be returned if they are faulty. Proof of purchase is required.

Cutting after "Opened headphones may be returned" loses both the fault condition and the proof-of-purchase requirement. A chunk needs enough surrounding text for its rule to make sense. A whole handbook can bury that rule among unrelated topics; a fragment can lose what makes it apply.

Cutting after 'Opened headphones may be returned' removes the fault condition and proof-of-purchase requirement. Keeping both sentences preserves the complete rule.
A useful passage keeps the rule and its conditions together.

Prepare the passages for meaning search

Our customer says "bring them back". The handbook says "returned". Searching only for matching words could miss the connection.

An embedding model turns each passage into a list of numbers, called an embedding or vector. It is trained so texts with related meanings tend to receive similar lists, even when the words differ. An individual number does not usually have a readable label such as "headphones" or "returns".

The application saves each passage's original words and source reference alongside its embedding. A search index organises those records for lookup. The numbers help find a passage; its saved words supply the evidence for an answer. These records can be reused for later questions and updated when the handbook changes.

Search with a complete question

The customer has already supplied the product, timing and fault, so this question can go straight to search. If those details were spread across earlier chat messages, an optional AI step could first turn "Can I bring them back?" into a complete search question.

That step is called query rewriting. It should carry forward known details without inventing a receipt or anything else the customer has not said. Here, we can keep the original wording.

The application sends the complete question through the same embedding model used for the passages, then compares its list with the saved passage lists.

To picture the comparison, use invented one-number lists: the question is [25], an unopened-products passage is [24], and the faulty-headphones rule is [27]. These are teaching values, not measured model outputs. On a number line, the unopened rule is closer to the question.

Real embeddings contain many numbers and use a comparison method suited to the model. The same limitation remains: a close match can concern the wrong situation.

Invented one-number embeddings place an unopened-products passage at 24, the customer question at 25, and the faulty-headphones rule at 27. The closest passage concerns the wrong situation. Each passage remains linked to its original text and source.
The closest meaning match may concern the wrong situation. These numbers are invented teaching values.

Search for words as well as meaning

The question also contains the product code "X200". A word search can find that exact code, including in repair instructions or an unopened-products rule.

Each search can find something the other misses. The faulty-headphones rule might never name "X200"; a passage naming it might concern repairs. Combining word and meaning search is called hybrid search.

The application combines the searches' rankings into one list, keeping a shared passage record once if both searches found it. We now have candidates about unopened returns, X200 repairs and faulty headphones. Which will actually help answer the customer?

Read the shortlist before selecting evidence

Suppose the combined list places unopened returns first, repairs second and the faulty-headphones rule third. An optional reranking step compares the complete search question with each candidate's actual words, then estimates which passages are most relevant.

For this small example, the application will send one leading passage to AI. Without a better order, it would send the unopened-products rule. A useful reranking moves the faulty-headphones rule to the top, so the selected evidence includes the fault condition and proof-of-purchase requirement.

That one-passage choice is only for this demonstration. Other questions may need several passages or sources.

A reranker can still get the order wrong. It only considers the candidates supplied; it cannot find a missing policy or restore a condition cut out during chunking. It also adds processing, so test whether it improves the evidence selected for your questions.

The combined shortlist initially ranks unopened returns first, X200 repairs second and faulty-headphones returns third. For an illustrative one-passage selection, the unopened rule would reach AI. After a useful reranking, the faulty-headphones rule moves to first and is the selected evidence; the other passages are excluded.
Here, changing the order changes which rule reaches AI. One passage is a demonstration choice.

Give AI the question and the evidence

The application sends the customer's original question, the selected readable passage and its source reference, with instructions to preserve the policy's conditions and say when evidence is missing. If the question depends on earlier chat messages, it includes those known details too.

A supported reply could be:

According to the shop's returns policy, opened headphones may be returned if they are faulty, and proof of purchase is required. Do you have a receipt or another proof of purchase? [Source 1]

The application can display Source 1 as a link to the handbook's Returns section. The link lets the customer check the source; the passage still has to support each claim.

The policy says nothing about a cash refund or its timing. An answer promising a full cash refund today without proof of purchase would add unsupported promises and contradict the proof-of-purchase requirement.

The policy allows returns of faulty opened headphones and requires proof of purchase. A supported reply retains both conditions. A promise of a full cash refund today without proof of purchase contradicts the proof requirement and invents a refund method and timing.
Check each promise against the passage, even when the answer includes a source link.

Check where an answer went wrong

Keep the customer question, selected passages and answer visible together. Then ask two questions:

Did the needed rule reach AI? If the fault condition or proof-of-purchase requirement is missing, inspect the chunk boundaries and search results. Check that the policy is current and applies to this product.

Did the answer preserve that rule? If AI received the complete passage but promised a cash refund today without proof of purchase, the answer went beyond its evidence. Open the cited section and compare the wording.

Try a question the passage cannot settle, such as "Will I receive a cash refund today?" The assistant should acknowledge what the policy does not establish.

These checks give you a place to start: fix the supplied evidence when it is incomplete, and investigate the answer when it misuses evidence that was available.

My idea and story, with AI helping shape the structure and revise the wording. I was hardly going to leave AI out of an article about AI.

About the author

I’m Rama, an AI engineer and product builder. I turn complicated AI ideas into explanations and systems that people can understand, trust and use.

More about me

Read next

How does the internet fit inside an LLM?

Training is like squeezing a huge library into a small suitcase. The pages do not travel with it, but many patterns do.

Continue reading →