Blog/Engineering

Engineering

Hybrid search for code: why we combine a graph, vectors and keywords

SnehaAI Engineer, EnWithAI

3 min read

Talk to us

What each kind of search finds

  1. KeywordsExact names: `PAYOUT-CAP`, an error code
  2. VectorsMeaning: “where do we cap refunds?”
  3. GraphConnections: who calls this, across repos
  4. FusedOne ranked list the agent can cite

Each method fails on a different question. Together they fail less.

When an agent gets an answer about your code wrong, the model is rarely the problem. It was handed the wrong code. Retrieval decides what the agent sees, and for code, no single kind of search is enough.

The short answer

AI agents working on code need hybrid search: keyword search for exact identifiers, vector search for meaning, and a code knowledge graph for relationships such as call chains across services. The results are fused into one ranked list, and answers cite where they came from.

Three questions, three failures

Here are three questions an engineer might ask about a payments system, and the search method that fails each one.

Search methods and the questions they missKeyword searchVector searchGraph traversal
“Where is REFUND_LIMIT_EXCEEDED raised?”Finds it exactlyMay return similar errors insteadNeeds a starting node
“How do we stop refunds above the original amount?”Misses code without those wordsFinds the check by meaningNeeds a starting node
“What breaks if I change this function?”Finds text, not callersFinds similar code, not callersFollows calls across services

Embeddings are good at meaning and bad at exact strings. Keyword search is the reverse. And neither knows that a function in one repository is called by a handler in another. Code is a network, and only a graph sees the network.

Building the three indexes

  1. Parse, do not just chunk

    Source files are parsed into their structure (modules, classes, functions, entry points) so chunks follow the code’s own boundaries instead of a fixed number of lines.

  2. Keyword index

    Identifiers, error codes, config keys and comments, searchable exactly.

  3. Vector index

    Embeddings of each unit of code and its documentation, for questions asked in plain language.

  4. Knowledge graph

    Nodes for services, modules, functions and domain entities; edges for calls, imports and data flow, including calls between services.

Fusing the results

Three ranked lists are not useful to an agent. We combine them with reciprocal rank fusion: each result scores by its rank in each list, so something ranked well by two methods beats something ranked first by only one. It needs no tuning of scores across very different systems, which is why it holds up in practice.

The graph then expands the top results. If the search finds the refund check, the graph adds the functions that call it and the service that exposes it, so the agent sees the change in context rather than as an isolated snippet.

Good retrieval does not give the agent more code. It gives it the right neighbors.

Answers that cite their sources

Every passage the agent uses keeps a pointer to its file and lines. When it answers a question or drafts an impact analysis, it cites them. That turns “the agent says so” into something a reviewer can check in seconds, and it makes wrong answers easy to spot.

What we measure

  • Recall at k on a fixed set of real questions with known answers: did the right code appear in the top results?
  • Citation accuracy: does the cited code actually support the claim?
  • Cross-repo hits: for impact questions, did retrieval find the affected services outside the starting repository?

We rerun the set whenever an index, a model or a fusion setting changes, so a retrieval regression shows up before an agent builds on it.

In CrewPE

CrewPE reads your repositories into a knowledge graph with keyword and vector indexes alongside, so specs, impact analysis and answers name every service a change touches.

See how CrewPE works

Questions teams ask

What is hybrid search for code?
Hybrid search combines more than one retrieval method, usually keyword search for exact identifiers, vector search for meaning, and for code a knowledge graph for relationships such as calls and imports. The results are merged into one ranked list.
Why is vector search not enough for code?
Embeddings match meaning, so they can miss exact names such as an error code or a config key, and they do not know which functions call each other. Code questions often need both exact matches and relationships.
What is reciprocal rank fusion?
A way to merge ranked lists by giving each result a score based on its rank in each list. Results ranked well by several methods rise to the top, without having to compare raw scores from very different search systems.

Sources

Product and pricing details change often. Check each vendor’s own pages before you decide.

  1. Cormack, Clarke and Büttcher, “Reciprocal Rank Fusion outperforms Condorcet and individual rank learning methods”, SIGIR 2009

Sneha

AI engineer at EnWithAI, working on CrewPE’s retrieval, grounding and agent evaluation.

Keep reading

Bring one real change. See how it would be specified, built and proven.

Start with a 45-minute call about your system: how it is built, how work is agreed today, and a sensible first step.