SubQueryExpansion
Models & Types · AiDotNet.RetrievalAugmentedGeneration.QueryExpansion
Expands complex queries by decomposing them into simpler, focused sub-queries for parallel retrieval.
Inheritance: QueryExpansionBase → SubQueryExpansion
For Beginners
Think of this like breaking a big question into smaller, easier ones:
Complex query: “Explain machine learning, deep learning, and reinforcement learning”
Sub-query decomposition:
- “Explain machine learning”
- “Explain deep learning”
- “Explain reinforcement learning”
- “information about machine learning” (key concept)
- “information about deep learning” (key concept)
Each sub-query finds specific documents, then combines all results!
var expander = new SubQueryExpansion(
llmEndpoint: "http://localhost:1234/v1",
llmApiKey: "your-key",
maxSubQueries: 4
);
var queries = expander.ExpandQuery(
"What is photosynthesis and how do plants use it for energy production?"
);
// Returns: ["What is photosynthesis?", "how do plants use it for energy production?",
// "information about photosynthesis", "information about energy production"]
Why use SubQueryExpansion:
- Handles multi-part questions effectively
- Each sub-query is more precise (better matches)
- Covers all aspects of complex information needs
- Ideal for research questions, comprehensive queries
When NOT to use it:
- Simple, single-concept queries (unnecessary overhead)
- When you need documents covering ALL aspects together (decomposition loses connections)
- Very short queries (nothing to decompose)
- When retrieval latency is critical (multiple searches = slower)
How It Works
SubQueryExpansion solves the “complex query problem” where a single query contains multiple information needs. It intelligently detects complexity indicators (conjunctions, multiple questions, comma-separated concepts) and decomposes the query into independent sub-queries that are easier to retrieve for. For example, “How does climate change affect polar bears and what conservation efforts exist?” becomes two focused queries. This approach improves both precision (each sub-query is more specific) and recall (broader topic coverage). The implementation uses linguistic patterns to identify sub-queries but can integrate with LLMs for more sophisticated decomposition. Results from all sub-queries are retrieved independently and merged to provide comprehensive coverage.
Constructors
| Constructor | Summary |
|---|---|
SubQueryExpansion(string llmEndpoint, string llmApiKey, int maxSubQueries) |
Initializes a new instance of the SubQueryExpansion class. |
Methods
| Method | Summary |
|---|---|
List<string> ExpandQuery(string query) |