Foundations to Frontier
Tests
F. NLP and Large Language Models › F10 Retrieval-Augmented Generation

Advanced RAG: query rewriting and graph RAG

Basic RAG retrieves once with the user's words. That fails when the question uses different words from the documents, or when the answer needs facts from several passages that the question does not all name. Advanced RAG rewrites, expands or decomposes the query, retrieves iteratively, or builds a knowledge graph from the documents and answers by traversing it. This page covers query rewriting, HyDE, multi-query fusion, decomposition and iterative retrieval, and graph RAG with local and global search. In the exercises you write query expansion, a triple graph and multi-hop traversal. You then answer 500 two-hop questions of the form 'in which city is this person's employer headquartered?'. Single retrieval succeeded under 5% of the time even with 20 passages. Decomposing into two retrievals reached 57%, and also rewriting nicknames reached 88%. A pre-built graph needed one lookup but reached 76%, because 10% extraction errors compound over two hops.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Asking the librarian a better question

Ask a librarian ‘where is Sam’s company based?’ and they may find Sam’s file but not the company’s address, because your question never named the company. A good researcher splits the question: first find Sam’s employer, then look up that company. They also translate nicknames and jargon into the words the documents use. Graph RAG goes further: it reads the whole library in advance and draws a map of who is connected to what. The demo shows how fusing several searches finds more.

Retrieval lab
BM25: D3 D1 D7 D2 D5 | dense: D1 D4 D3 D6 D2
Fused order: D1 D3 D2 D4 D7 D6 D5. Documents ranked well by both lists rise to the top.
Why engineers care

Questions about engineering systems are often multi-hop: which subsystem uses this part, which supplier makes it, which other aircraft are affected.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A multi-hop question needs…
  2. 2. Query rewriting changes…
  3. 3. Graph RAG answers by…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Query rewriting: an LLM rewrites a conversational or vague question into a standalone search query; resolves pronouns, nicknames and jargon.
  • Expansion: add synonyms or related terms; HyDE embeds a hypothetical answer instead of the question; multi-query retrieval (RAG-Fusion) searches several rewrites and fuses them with RRF.
  • Decomposition and iterative retrieval: split a question into sub-questions, retrieve and read each, and use answers to form the next query (IRCoT, Self-Ask, ReAct-style agents in F11).
  • Routing: send each query to the right index or tool (SQL, documents, API).
  • Graph RAG: an LLM extracts entities and relations into a knowledge graph; local search follows edges from the question’s entities; global search summarises communities of the graph for corpus-wide questions (Microsoft GraphRAG, 2024).
  • Costs: every rewrite, hop and extraction is an LLM call; errors compound across steps.
📝 Quick check (Level 1)
0/3 answered
  1. 1. HyDE searches with…
  2. 2. RAG-Fusion fuses…
  3. 3. Global graph search uses…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Implementing advanced retrieval

Exercise 1.

  • Expand a query with synonyms.
  • Build a graph from triples.
  • Follow a chain of relations.

Practice.

  • LlamaIndex query transformations (HyDE, sub-question engine) and LangChain MultiQueryRetriever.
  • Microsoft GraphRAG, LlamaIndex PropertyGraphIndex, Neo4j for graph RAG.
  • Log every intermediate query and retrieval; multi-step pipelines fail in the middle.
  • Bound the number of hops and LLM calls; fall back to plain retrieval.
  • Evaluate graph extraction quality on a sample before relying on traversal.
def answer_multi_hop(question, llm, search, max_hops=3):
    notes = []
    for _ in range(max_hops):
        q = llm(f"Question: {question}\nKnown: {notes}\nNext search query, or DONE:")
        if q.strip() == "DONE": break
        notes.append(llm(f"From these passages, answer '{q}':\n" + "\n".join(search(q, 3))))
    return llm(f"Question: {question}\nFacts: {notes}\nAnswer:")
🐍 Exercise 1: expansion and graph traversal
Write synonym expansion, build a triple graph and follow a chain of relations.
📝 Quick check (Level 2)
0/3 answered
  1. 1. A sub-question engine is in…
  2. 2. Multi-step pipelines need…
  3. 3. Bound…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why single retrieval fails on multi-hop questions. The second-hop passage is about an entity (the company) that the question never mentions; it shares only generic words with the question, so it competes with every passage of its type. Raising k helps only in proportion to k divided by the number of such passages.

Compounding errors. A pipeline of independent steps succeeds with the product of their success probabilities: two reads at 95% give 90%; a graph with 90%-accurate edges answers two-hop questions 81% of the time. Decomposition pays its errors at question time; graph RAG pays them at indexing time, once, and every question inherits them.

Rewriting and vocabulary mismatch. Lexical retrieval needs the query’s words to appear in the passage. A rewrite that maps the user’s terms to the documents’ terms fixes mismatch, at the cost of an LLM call and the risk of a wrong rewrite.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Two independent 95% steps succeed with…
  2. 2. Graph RAG pays extraction errors…
  3. 3. Rewriting fixes…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How much do decomposition, query rewriting and a knowledge graph help on two-hop questions?

History.

  • L. Gao et al., “Precise zero-shot dense retrieval without relevance labels” (HyDE, 2022).
  • O. Press et al., “Self-Ask” (2022); H. Trivedi et al., “IRCoT: interleaving retrieval with chain-of-thought” (ACL 2023).
  • D. Edge et al., “From local to global: a graph RAG approach to query-focused summarization” (Microsoft, 2024).

Exercise 2. 2000 ‘person works for company’ and 300 ‘company headquartered in city’ passages with filler words, BM25 retrieval.

  • Questions: 500 ‘city of this person’s employer’; 40% name the person by a nickname absent from the passages.
  • Reader: extracts a fact from a passage correctly 95% of the time; rewriter: maps a nickname to the real name 90% of the time.
  • Graph: built offline from all passages; each extracted edge is right 90% of the time; lookup by exact name.
method accuracy retrieval or lookup calls
single retrieval, k = 5 0.004 1
single retrieval, k = 20 0.040 1
decompose into 2 retrievals 0.570 2
decompose + rewrite 0.878 2 (+1 LLM rewrite)
graph lookup 0.520 1
graph + rewrite 0.760 1 (+1 LLM rewrite)
  • Single retrieval failed: the headquarters passage shares only the word ‘headquartered’ with the question, as do 299 others.
  • Decomposition solved the multi-hop structure; its ceiling here was 0.95² ≈ 0.90 from the two reads.
  • Rewriting recovered the nickname questions, which every other method lost.
  • The graph was cheapest per question but capped at 0.9² = 0.81 by extraction errors.
  • Seeds 1 and 2: decompose + rewrite 0.856 and 0.876; graph + rewrite 0.774 and 0.822.
  • Caveat: the error rates are assumptions. Graph RAG’s main strength, global questions over a whole corpus, is not tested here.
🐍 Exercise 2 (research): how much do decomposition, query rewriting and a knowledge graph help on two-hop questions?
Answer two-hop questions with single retrieval, decomposition, query rewriting and a pre-built knowledge graph.

Advanced RAG in AI, robotics and aerospace

  • Configuration and parts graphs: aircraft parts, suppliers, assemblies and service bulletins form natural knowledge graphs for impact analysis.
  • Fault diagnosis: iterative retrieval follows symptom → fault → procedure chains across manuals.
  • Requirements traceability: requirement → design → test links answer ‘what is affected if this changes?’.

References

  1. L. Gao, X. Ma, J. Lin and J. Callan (2023). “Precise zero-shot dense retrieval without relevance labels.” ACL.
  2. O. Press et al. (2023). “Measuring and narrowing the compositionality gap in language models.” Findings of EMNLP.
  3. H. Trivedi et al. (2023). “Interleaving retrieval with chain-of-thought reasoning for knowledge-intensive multi-step questions.” ACL.
  4. D. Edge et al. (2024). “From local to global: a graph RAG approach to query-focused summarization.” arXiv:2404.16130.