liveBook · Manning

Query rewriting

As explained in AI Agents and Applications

Query rewriting is a technique that transforms an original search request into one or more clearer or more targeted queries to improve retrieval results.

A search request can fail even when the right information is available. A vague question may give a web search engine too little context; a poorly phrased question may match the wrong chunks in a vector store. Query rewriting puts a transformation step before retrieval: an LLM turns the original request into a clearer search query or several targeted queries. The results then support the answer. section 4.4 section 9.0

One request, three searches

For “How many titles did Michael Jordan win?”, a rewriter can generate “Michael Jordan NBA championships total”, “List of NBA titles won by Michael Jordan”, and “Michael Jordan basketball career championships count”. Searching each phrasing gathers results from several angles rather than relying on the original wording alone. section 4.4

For vector search, start with one rewrite. Prompt the LLM to return a query suited to the vector store, then pass that query—not the raw question—to the retriever. Keep the original question for answer synthesis. This is the Rewrite-Retrieve-Read pattern: rewriting improves the retrieval input without silently changing what the user asked the answer generator to address. section 9.1 section 9.1.4

Listing 9.2 Combined RAG chain with query rewriting
from langchain_core.runnables import RunnablePassthrough
retriever = uk_granular_collection.as_retriever()

rag_prompt_template = """
Given a question and some context, answer the question.
If you do not know the answer, just say I do not know.

Context: {context}
Question: {question}
"""

rag_prompt = ChatPromptTemplate.from_template(
    rag_prompt_template) 

rewrite_retrieve_read_rag_chain = (
    {
        "context": {"user_question": RunnablePassthrough()} 
            | rewriter_chain | retriever, #1
        "question": RunnablePassthrough(), #2
    }
    | rag_prompt
    | llm
    | StrOutputParser()
)

The RAG chain makes the two paths visible: the rewritten question supplies retrieved context, while the original question passes through to the answer prompt. section 9.1.4

Choose the transformation to fit the retrieval problem:

  • Use a single, more precise rewrite when wording is the main obstacle. A prompt can ask for a ChromaDB search query; a LangChain chain connects ChatPromptTemplate, ChatOpenAI, and StrOutputParser. section 9.1.2
  • Generate multiple phrasings when one search may miss relevant documents. Parse the LLM’s newline-separated alternatives into a list and retrieve for each; MultiQueryRetriever offers a standard setup. section 9.2.1 section 9.2.3
  • For more control over combined results, apply Reciprocal Rank Fusion to rerank documents across query-result lists before answer synthesis. section 10.7.4

More queries are not a free improvement: extra web searches increase response time if run sequentially, though parallel processing can reduce that cost. Rewriting or multiplying questions also may fail to produce the desired results. Test the approach against your own data before treating it as a retrieval fix. section 4.4 section 9.2.3 section 9.6

How AI Agents and Applications covers it

How deep it goes
Builds a prompt-to-LLM rewriter and a complete RAG chain, then extends generation to multiple parsed queries and retrieval.
Examples
Uses basketball-championship web searches and Cornwall travel questions retrieved from a vector store.
What it assumes
Sets up the rewriting chain explicitly but uses an ingested vector collection and an environment with Python dependencies and an API key.
What you can do after
Build it from scratch with a LangChain rewriter chain, or apply MultiQueryRetriever to retrieve across alternative questions.
How it connects
Connects Rewrite-Retrieve-Read and semantic search with multi-query retrieval, Retrieval-Augmented Generation, and Reciprocal Rank Fusion.

Also explained in

Compare how 5 books explain query rewriting →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI Agents and Applications. The section links point to the parts of the book it draws on.