1 min lesson
Combine exact and semantic search
Combine search methods so exact identifiers and conceptual matches can cover each other's gaps.
Step 1 of 2
Combine exact and semantic search
Cursor reports that its Agent uses grep and semantic search together, and that the combination performs better than either tool alone. Structural references can add another precise signal in a proposed design.
- Approach
- Lexical search
- Good at
- Exact identifiers, error strings and rare tokens
- Fails at
- Conceptual matches that use different words
- Approach
- Semantic search
- Good at
- Code related by meaning rather than exact text
- Fails at
- Exact identifiers and code changed after the index was built
- Approach
- Structural references
- Good at
- Definitions, types, imports and callers
- Fails at
- Relevant code with no static connection
| Approach | Good at | Fails at |
|---|---|---|
| Lexical search | Exact identifiers, error strings and rare tokens | Conceptual matches that use different words |
| Semantic search | Code related by meaning rather than exact text | Exact identifiers and code changed after the index was built |
| Structural references | Definitions, types, imports and callers | Relevant code with no static connection |
Each method covers a weakness in the others. Combine their candidates, then rank the merged set again.
Merge candidate sets
Run lexical and semantic searches, merge their candidates and remove duplicates. Rank the result with retrieval score, exact symbol matches, structural distance, current file state and task requirements. Measure the ranking on retrieval tasks with known relevant files or spans.