Query Rewrite / HyDE

What is Query Rewrite

Query Rewrite is a pre-retrieval technique that transforms the user’s original query into one or more alternative query forms before sending them to the retrieval system.
查询重写是一种检索前技术,在将用户原始查询发送到检索系统之前,将其转换为一种或多种替代查询形式。

 Imagine you’re asking a librarian for help. Instead of just saying “books about AI,” you might also say “machine learning texts,” “neural network resources,” and “deep learning guides” — because different books use different terminology. Query Rewrite is the LLM doing exactly that: generating multiple ways to ask the same question so the search system has better chances of finding relevant documents.

CN: 想象你在问图书管理员。你不仅说“AI相关的书”,还会说“机器学习书籍”、“神经网络资料”、“深度学习指南”——因为不同的书用不同的术语。查询重写就是让LLM做同样的事:生成多种问法,让检索系统有更大机会找到相关文档。

WHat is HyDE

Hypothetical Document Embeddings,假设性文档嵌入

HyDE is a specific query rewriting technique that generates a hypothetical (fake) answer document to the user’s query first, then embeds that generated document for similarity search — instead of embedding the query directly.

CN: HyDE 是一种特定的查询重写技术,它先生成一个假设性(虚构的)答案文档来回答用户查询,然后对该生成文档做嵌入用于相似度搜索——而不是直接嵌入原始查询。

Instead of asking “where can I find Italian food?” and searching for that question, HyDE first writes a fake Yelp review: “This Italian restaurant has amazing pasta and tiramisu…” — then searches for documents that look like that review. Because the review looks more like actual documents in the database than the question does.

CN: 不是直接问“哪里有好吃的意大利菜?”然后去搜这个问题。HyDE 先写一篇假的Yelp评论:“这家意大利餐厅的意面和提拉米苏超赞…” —— 然后去搜和这篇评论相似的文档。因为评论比问题本身更像数据库里的真实文档。

Query Rewrite 的核心内容

4.1 查询重写的四种主要类型

类型ENCN
改写 (Rewriting)Rephrase the query for better embedding alignment改写查询以更好地对齐嵌入
扩展 (Expansion)Add semantically related terms添加语义相关的术语
分解 (Decomposition)Split complex queries into sub-queries将复杂查询拆分为子查询
多路生成 (Multi-Query)Generate multiple query variants生成多个查询变体

4.2 多查询检索 (Multi-Query Retrieval) – Fan-Out

EN: Generate multiple query variants from the original, run them in parallel against the vector DB, merge and deduplicate results. This covers more angles of the user’s intent.

CN: 从原始查询生成多个变体,并行发送到向量数据库,合并并去重结果。这覆盖了用户意图的更多角度。

4.3 RRF (Reciprocal Rank Fusion)

EN: When merging results from multiple query variants, RRF scores documents by their rank positions rather than raw similarity scores — documents that appear high in multiple result lists get boosted.

CN: 合并多路查询结果时,RRF 按文档的排名位置而非原始相似度分数来打分——在多路结果中都排名靠前的文档获得加成。

HyDE works step

HyDE works in two steps:

  1. Generate: Given a query, zero-shot prompt an LLM to generate a hypothetical document that answers the query
  2. Embed & Retrieve: Encode that hypothetical document and use it to retrieve real documents via vector similarity

CN: HyDE 分两步工作:

  1. 生成: 给定查询,用零样本提示让 LLM 生成一个回答该查询的假设性文档
  2. 嵌入与检索: 编码该假设性文档,用其向量通过相似度检索真实文档

Why this works: The hypothetical document is in the same style as real documents (declarative, detailed, expository). So its embedding lands closer to real document embeddings than the query embedding would.

CN – 为什么有效: 假设性文档与真实文档风格相同(陈述句、详细、说明性)。所以它的向量比查询向量更接近真实文档的向量。

Query Rewrite vs HyDE 对比

维度ENCN
Query RewriteRewrites the question改写问题
HyDEGenerates a fake answer then embeds that生成假答案然后嵌入假答案
Query Rewrite 目标Make the query easier to retrieve让查询更容易被检索
HyDE 目标Make the embedding closer to document space让向量更接近文档空间
Query Rewrite 开销1 LLM call + N vector searches1次LLM调用 + N次向量搜索
HyDE 开销1 LLM call + 1 vector search1次LLM调用 + 1次向量搜索

Key Takeaways

要点ENCN
Query Rewrite 是检索前技术Query Rewrite is a pre-retrieval technique查询重写是检索前技术
HyDE 生成假设文档再嵌入HyDE generates hypothetical doc then embeds itHyDE 生成假设文档再嵌入
HyDE 论文推荐生成5个文档取平均HyDE paper recommends generating 5 docs and averagingHyDE论文推荐生成5个文档取平均
HyDE 的温度推荐0.7HyDE recommended temperature is 0.7HyDE推荐temperature为0.7
Multi-Query 用 asyncio.gather() 并行Use asyncio.gather() for parallel multi-query用 asyncio.gather() 实现多查询并行
RRF 按排名位置而非分数融合RRF fuses by rank position, not raw scoresRRF按排名位置而非原始分数融合
HyDE 弥合”问题空间”与”答案空间”的鸿沟HyDE bridges the “question space” vs “answer space” gapHyDE弥合”问题空间”与”答案空间”的鸿沟
Query Rewrite 提升召回率,HyDE 提升精确率Query Rewrite improves recall, HyDE improves precision查询重写提升召回率,HyDE提升精确率

Advanced RAG

What is Advanced RAG?

Advanced RAG (Advanced Retrieval-Augmented Generation) is an evolutionary upgrade over the basic “Naive RAG” pipeline. It adds a suite of optimization techniques at every stage of the RAG workflow — pre-retrieval, retrieval, post-retrieval, and evaluation — to systematically improve retrieval precision, recall, and generation quality.

Naive RAG is like sending a junior intern to the library with a single, vague question, grabbing the first 5 books they find, and copying paragraphs directly. Advanced RAG is like sending a team of expert researchers who:

  • Rephrase the question in 5 different ways (Query Rewrite)
  • Write a fake “perfect answer” first to know exactly what to look for (HyDE)
  • Search both the card catalog AND the full-text index (Hybrid Search)
  • Have a senior editor re-rank the top results (Reranker)
  • Summarize and compress the findings before presenting them (Context Compression)
  • And finally, run a quality check on the entire process (Evaluation)

CN: 朴素 RAG 就像派一个初级实习生去图书馆,带着一个模糊的问题,抓起前5本书,直接抄段落。Advanced RAG 就像派一队专家研究员:

  • 把问题用5种不同方式重新表述(查询重写)
  • 先写一篇“完美的假答案”来明确要找什么(HyDE)
  • 同时搜索卡片目录和全文索引(混合检索)
  • 让高级编辑对结果重新排名(重排序器)
  • 在呈现之前总结和压缩发现(上下文压缩)
  • 最后对整个流程做质量检查(评估)

What does Advanced RAG include?

Layer 1: Chunking Strategies

Chunking 策略

ENCN
Recursive Chunking: Split text by paragraphs/sentences with overlap递归分块:按段落/句子分割,带重叠
Semantic Chunking: Split by semantic boundaries using embedding similarity语义分块:用嵌入相似度按语义边界分割
Parent-Child Retrieval: Retrieve child chunks, return parent chunks for context父子检索:检索子块,返回父块作为上下文

Layer 2:Retrieval Strategies

检索策略

ENCN
Hybrid Search: Combine dense (vector) + sparse (BM25) retrieval混合检索:结合稠密(向量)+ 稀疏(BM25)检索
RRF (Reciprocal Rank Fusion): Merge multi-strategy results by rankRRF(倒数排名融合):按排名位置合并多路结果
Vector DB Selection: Chroma, Pinecone, Milvus, Azure AI Search向量数据库选型:Chroma, Pinecone, Milvus, Azure AI Search

Layer 3: Post-Retrieval Optimization

检索后优化

ENCN
Reranker: Cross-encoder models to re-score retrieved documents重排序器:用交叉编码器模型重新给检索文档打分
Query Rewrite: Rephrase queries for better embedding alignment查询重写:改写查询以更好地对齐嵌入
HyDE: Generate hypothetical answer → embed that → retrieveHyDE:生成假设答案 → 嵌入假设答案 → 检索
Multi-Query Retrieval: Generate multiple query variants, search in parallel多查询检索:生成多个查询变体,并行搜索
Context Compression: Summarize or filter retrieved docs to fit context window上下文压缩:总结或过滤检索文档以适应上下文窗口

Layer 4: Evaluation

评估体系

ENCN
RAG Eval Metrics (ragas): Context Relevancy, Answer Relevancy, FaithfulnessRAG评估指标(ragas):上下文相关性、答案相关性、忠实度
Golden Dataset: Human-annotated Q&A pairs for regression testing黄金数据集:人工标注的问答对,用于回归测试

Advanced RAG differences from Naive RAG

维度ENCN
检索次数Naive: 1 single retrieval call. Advanced: Multiple parallel retrievals + reranking.朴素:单次检索调用。Advanced:多次并行检索 + 重排序。
查询处理Naive: Embed query directly. Advanced: Query Rewrite / HyDE / Multi-Query.朴素:直接嵌入查询。Advanced:查询重写 / HyDE / 多查询。
检索类型Naive: Vector search only. Advanced: Hybrid (Vector + BM25 + RRF).朴素:仅向量搜索。Advanced:混合(向量 + BM25 + RRF)。
排序Naive: Raw similarity scores. Advanced: Cross-encoder reranker (more accurate).朴素:原始相似度分数。Advanced:交叉编码器重排序器(更精确)。
分块Naive: Fixed-size chunking (e.g., 512 tokens). Advanced: Semantic / Parent-Child.朴素:固定大小分块(如512 token)。Advanced:语义 / 父子分块。
上下文窗口利用Naive: Dump all retrieved chunks into prompt. Advanced: Compress / filter / summarize.朴素:把所有检索块塞进提示词。Advanced:压缩 / 过滤 / 总结。
评估Naive: None or manual. Advanced: Automated Eval with ragas + Golden Dataset.朴素:无或手动。Advanced:用 ragas + 黄金数据集自动评估。
失败处理Naive: If retrieval fails, LLM hallucinates. Advanced: Query rewrite retries, multi-query fallback.朴素:检索失败则LLM幻觉。Advanced:查询重写重试、多查询降级。

Example: You’re building a customer support RAG for a cloud provider’s documentation.

阶段Naive RAGAdvanced RAG
用户查询“how fix 503 error”“how fix 503 error”
处理直接嵌入查询Query Rewrite → “how to troubleshoot HTTP 503 service unavailable error”, “503 error resolution steps”
检索向量搜索返回5个块,其中3个是关于”503″但不相关的(如负载均衡器通用文档)混合检索(BM25+向量) + RRF,返回15个候选
重排Cross-encoder reranker 重新打分,选出最相关的3个块
上下文5个块全部塞入(可能超过窗口)Context compression 总结每个块,只保留关键步骤
生成答案可能泛泛而谈答案精确指出: “检查service mesh sidecar, 重启x, 检查Y”