The question a user types is often a poor search query: it can be vague, conversational, or several questions in one. Query transformation rewrites the question before retrieving.
Methods
- rewriting: turn a follow-up such as "and for corporates?" into a self-contained query, using the chat history
- multi-query: generate several phrasings, retrieve for each, and fuse the lists with Reciprocal Rank Fusion (RRF)
- decomposition: split a multi-part question into sub-questions and retrieve for each
- HyDE: ask the LLM to draft a hypothetical answer and search with that, since answers look more like passages than questions do, so the search lands closer (Gao et al. 2022)
Cost
- each trick costs an extra LLM call
- keep a trick only if Recall@k improves
When to use
- the question depends on earlier turns of the chat
- a hard question that every retriever misses, for example "What are warning signs in letters of credit and import export deals?" when the passage says "trade finance red flags" and "over-invoicing"
Code
A simple version of multi-query: write extra phrasings by hand (or with an LLM), search with each, and fuse the results.
def multi_query_search(queries, k=50):
"""Run hybrid search for every phrasing, then fuse the ranked lists with RRF."""
order, _ = rrf([hybrid_search(q) for q in queries])
return order[:k]
queries = [
"What are warning signs in letters of credit and import export deals?",
"trade finance red flags invoice shipping documents", # added words that the passage uses
]
results = multi_query_search(queries)Notes:
- the original question stays in the list, so a good first phrasing is never lost
rrfandhybrid_searchare the functions from the fusion step- check whether the rank of the target passage improves, using Recall@k and MRR
Retrieval also decides who is allowed to see what, see Metadata Filtering and Access Control.