Lesson 3 of 4 Advanced Retrieval Techniques

Transforming Queries with Rewriting, Expansion and Decomposition

This lesson covers query transformation techniques that turn short, ambiguous, conversational or compound user questions into queries that retrieve well.

Learning outcomes

After completing this lesson, you will be able to:

  • Rewrite conversational follow-up questions.
  • Expand queries to improve recall.
  • Decompose compound questions with Step Functions.

Query rewriting

Users rarely phrase questions in the way that retrieves documents best. In a conversation, follow-up questions depend on earlier turns: "And what about for part-time staff?" means nothing to a search engine on its own. Query rewriting uses an FM, given the recent conversation history, to produce a standalone query that can be searched, such as "What is the parental leave policy for part-time staff?".

Rewriting can also correct spelling, expand abbreviations and remove conversational filler such as greetings, all of which improve retrieval. A small, fast model is usually sufficient, because the task is simple and adds latency to every request. The rewritten query should be logged alongside the original, because when an answer is wrong, the cause is often a rewrite that changed the meaning of the question. Bedrock Knowledge Bases can perform this step automatically when RetrieveAndGenerate is given a session ID.

The following diagram shows a follow-up question rewritten into a standalone query.

Query rewriting in a conversation. A small, fast model reads the recent history and turns a follow-up such as “And what about for part-time staff?” into a standalone query that retrieval can use: “What is the parental leave policy for part-time staff?”
The rewrite adds one small model call to every request.
Put in order

A decomposed query

Exam skills 1.5.5

Put the steps of handling a comparison question with query decomposition in order.

Log in to try this activity Interactive activities are free with an account.

Query expansion

Query expansion improves recall, the share of relevant documents found, by searching for more than the user's exact words. It can add synonyms and domain terms, for example searching for "PTO" when the user asks about "annual leave". It can generate several paraphrases of the question, search each in parallel and merge the results. Or it can use a hypothetical document, a technique known as HyDE: the FM writes a plausible answer to the question, and that answer, rather than the short question, is embedded and used for search, because it resembles the wording of real documents more closely.

Expansion has a cost. Each additional query adds latency and retrieval cost, and broader searches return more irrelevant passages along with the relevant ones. Expansion is therefore usually combined with reranking, which removes the noise before the passages reach the model, and its benefit is confirmed with a retrieval test set rather than assumed.

Query decomposition

Compound questions, such as "Compare the warranty terms of the X200 and the X300 and say which covers water damage", need information from several places. Query decomposition splits them into sub-questions, retrieves for each and combines the results before generation. Bedrock Knowledge Bases provide decomposition in RetrieveAndGenerate through the orchestration configuration.

"knowledgeBaseConfiguration": {
  "knowledgeBaseId": "KB12345678",
  "modelArn": "...",
  "orchestrationConfiguration": {"queryTransformationConfiguration": {"type": "QUERY_DECOMPOSITION"}}
}

Custom decomposition is implemented in a Lambda function that asks an FM for sub-questions as JSON, or in Step Functions: a task classifies and rewrites the query, a Map state retrieves for each sub-question in parallel, a task merges and deduplicates the chunks, and a final task generates the answer. Step Functions makes each transformation visible and retryable.

The following table summarises the techniques.

TechniqueSolvesImplementation
RewritingFollow-ups and messy queriesSmall FM with recent history
Expansion (synonyms, paraphrases, HyDE)Low recall from short or unusual wordingFM generates variants; merge and rerank
DecompositionCompound and comparative questionsKnowledge Bases option, Lambda or Step Functions Map
Filter extractionQuestions with implicit constraintsImplicit filtering or FM-extracted metadata filters

The following diagram shows a comparison question decomposed into one sub-query for each subscription plan.

Query decomposition for comparison questions. The question about Basic and Premium cancellation fees is split into one sub-query per plan, each is retrieved separately, and the combined context contains both plans' fees before one answer is generated.
In Step Functions, the Map state retrieves the sub-queries in parallel.
Match the pairs

Which query technique?

Exam skills 1.5.5

Match each user query with the technique that improves its retrieval most.

Log in to try this activity Interactive activities are free with an account.
Worked example

Comparison questions

Users ask questions such as "How do the cancellation fees for the Basic and Premium plans differ?" Answers often describe only one plan.

A single query retrieves chunks about whichever plan is semantically closest. Enabling query decomposition in RetrieveAndGenerate, or a Step Functions workflow that generates one sub-query per plan and retrieves them in a Map state, ensures both plans' fees are in the context.

Check yourself

A user asks a follow-up question: "What about the second one?" What should happen before retrieval?

Show answer

Query rewriting, which uses the conversation history to turn it into a standalone query.

Exam Tips

Remember for the exam

  • Follow-up questions: rewrite into standalone queries using history.
  • Short or unusual wording: query expansion, often with HyDE, plus reranking.
  • Compound or comparative questions: query decomposition.
  • Visible, parallel, multi-step transformation: Step Functions with a Map state.