Query Optimization
Use Single, Focused Queries
Start with one semantic query to minimize cost and latency. Multiple queries are expensive and should only be used for complex, multi-faceted questions.Good
Avoid
If one query doesn’t return enough results, make multiple separate search calls instead of adding more queries to a single call.
Combine Semantic + Regex
Use natural language for semantic search, then filter with precise regex patterns for the best results.Example
- Semantic search: Finds conceptually relevant documentation
- Regex filtering: Narrows results to specific code patterns
Pattern Writing Tips
Case Sensitivity
By default, patterns are case-sensitive. Use(?i) prefix for case-insensitive matching.
authentication→ matches “authentication” only(?i)authentication→ matches “Authentication”, “AUTHENTICATION”, “authentication”
Common Patterns
\w+ - Word Characters
\w+ - Word Characters
Matches letters, digits, and underscoresExample:
function\s+\w+ matches function names\s+ - Whitespace
\s+ - Whitespace
Matches spaces, tabs, newlinesExample:
async\s+def matches async function definitions.* - Any Characters
.* - Any Characters
Matches any character (greedy)Example:
function.*{ matches from “function” to opening bracea|b - Alternation
a|b - Alternation
Matches “a” OR “b”Example:
GET|POST|PUT|DELETE matches HTTP methods[abc] - Character Class
[abc] - Character Class
Matches any single character in the setExample:
[Aa]uth matches “auth” or “Auth”Escaping Special Characters
Escape special regex characters with a backslash:| Character | Escaped | Usage |
|---|---|---|
. | \. | Literal dot |
( ) | \( \) | Literal parentheses |
[ ] | \[ \] | Literal brackets |
{ } | \{ \} | Literal braces |
\ | \\ | Literal backslash |
interface{} in Go:
Cost & Performance
Performance TipsThese practices help reduce costs and improve response times:
- Cache results for frequently searched patterns
- Use specific regex patterns to reduce result size
- Avoid overly broad patterns that match everything
- Start with narrow queries and broaden if needed
Cost Optimization Workflow
1
Start with Focused Query
Use a single, specific semantic query:
2
Apply Precise Pattern
Use a targeted regex pattern:
3
Refine if Needed
If results are insufficient, broaden the pattern or make additional calls
Pattern Specificity
Balance between too broad and too narrow:Too Broad
Too Narrow
Just Right
With Context
Real-World Scenarios
Finding API Endpoints
Database Queries
Error Handling
Documentation Quality Tips
Be Specific
Ask focused questions about specific features or patterns
Use Keywords
Include relevant technical terms in your semantic queries
Test Patterns
Validate regex patterns at regex101.com before using
Iterate
Start broad, then refine based on initial results