Fixing the search before it happens

Advanced RAG keeps naive RAG's overall shape (chunk, embed, retrieve, generate) but adds optimization steps before and after the core retrieval call. This lesson covers the "before" half: pre-retrieval optimization, changes made to the query or the index before a single similarity search runs.

Query rewriting

A user's raw question is often a poor embedding target: short, ambiguous, or phrased casually ("how do I get my money back" instead of "refund policy for digital purchases"). Query rewriting uses a language model to expand or reformulate the question into a form that embeds closer to how the source documents are actually written, before that rewritten query is used for retrieval. A related technique, HyDE (Hypothetical Document Embeddings), goes a step further: it asks the model to write a hypothetical, plausible-sounding answer to the question first, then embeds that hypothetical answer instead of the question itself, on the theory that an answer-shaped piece of text embeds closer to a real answer-shaped chunk than a question-shaped one does.

Better chunk sizing

Instead of naive RAG's one fixed chunk size for every document, advanced RAG chunks by structure: splitting on headings, paragraphs, or semantic boundaries detected by a model, so a chunk is far more likely to be one complete, coherent unit of meaning. A common refinement is sentence-window retrieval: index small, precise chunks for matching accuracy, but at retrieval time pull in the surrounding sentences too, so the model gets tight matching and enough context in the same step.

Raw Question

Rewritten / HyDE Query

Retrieval

Documents

Structure-aware Chunks

Two independent fixes applied before the similarity search runs: a better query, and better chunk boundaries.

Checkpoint

  • Query rewriting: reformulating a user's raw question into a form that embeds closer to how source documents are written.
  • HyDE: embedding a hypothetical answer to the question instead of the question itself, to match answer-shaped chunks better.
  • Structure-aware chunking splits on real boundaries (headings, paragraphs) instead of a fixed token count, avoiding meaning cut in half.

If anything here still feels unclear, ask before moving to Lesson 7.