How do you implement query understanding and expansion for better RAG retrieval?
Query understanding improves retrieval by transforming user queries before searching. Intent classification: detect query type (factual question, comparison, how-to, troubleshooting, opinion-seeking) to adjust retrieval strategy and response format. Query expansion: generate multiple reformulations of the original query using an LLM to capture different aspects and phrasings, then retrieve using all variants and merge results. Hypothetical Document Embedding (HyDE): generate a hypothetical answer to the query using an LLM (without retrieval), embed the hypothetical answer, and use it as the search vector. This often retrieves better results because the hypothetical answer is more similar to actual answer passages than the question alone. Sub-question decomposition: for complex queries, decompose into simpler sub-questions, retrieve for each sub-question independently, and synthesize answers from multiple retrieval sets. Query routing: classify queries to determine which knowledge base, index, or retrieval strategy to use (e.g., technical questions route to documentation index, policy questions route to policy index). Filter extraction: parse metadata filters from natural language (extract date ranges, categories, product names from the query), and apply as pre-filters before vector search. Spell correction and normalization: handle typos, abbreviations, and acronyms before retrieval. Conversation context integration: for multi-turn conversations, resolve pronouns and references by incorporating relevant conversation context into the query. Implementation: create a query preprocessing pipeline that runs before retrieval, use an LLM for complex transformations (expansion, decomposition, HyDE), and use rules for simple transformations (spell check, filter extraction). Evaluation: measure retrieval quality with and without query understanding, track which techniques improve which query types, and optimize the pipeline based on data.