Graph-Enhanced Retrieval-Augmented Generation: Principles and Practice
This article explains how Graph-Enhanced Retrieval-Augmented Generation (RAG) overcomes the limits of vector-only retrieval, compares global and local query paths, walks through the six-step local retrieval pipeline, and covers generation-stage context organization plus engineering and evaluation practices.
1. Why Graph-Enhanced RAG Is Needed
Traditional retrieval-augmented generation relies on vector similarity to recall text chunks. This works well for questions about a specific local fact, but it breaks down when an answer requires synthesis across documents and paragraphs. Consider questions about the central conflict of a long novel, the overall architectural style of a large code repository, or the common root causes across a batch of incident reports. No single chunk can answer these questions. The scattered information must first be aggregated into an intermediate layer of abstraction, and the answer must be built on that abstraction.
The core idea of graph-enhanced RAG is to build a structured index layer on top of the raw corpus. Entities, relationships between entities, and community summaries built around entities are distilled into that layer. At retrieval time, the system no longer performs nearest-neighbor search only in the original vector space; instead, it performs targeted expansion across a multi-layer structure of “entity–relationship–community–source text.”
This approach delivers three benefits:
- Retrieved results carry structural constraints, which reduces semantic drift.
- Community summaries are naturally aggregated intermediate conclusions and can directly support global questions.
- The evidence chain is traceable, so every conclusion can be traced back to a specific source chunk.
2. How the Two Query Paths Divide the Work
Global search targets the dataset as a whole and produces summarizing judgments. Its prerequisite is that community detection and community report generation have already been completed offline. The online stage only needs to aggregate results layer by layer. As a result, responses are stable, but index construction is costly.
Local search targets a specific object, such as “describe a particular character” or “describe a particular service module.” It starts from the query, locates the relevant entities, retrieves surrounding evidence along the graph structure, and finally hands the material to the LLM to compose an answer. Its index is lightweight and friendly to incremental updates, but it is sensitive to how the query is phrased.
In engineering practice the two paths are complementary: the global path covers the “big picture” and the local path covers the “specific point.” Upper-layer applications can route requests based on the intent behind a question.
3. The Six-Step Local Retrieval Pipeline
Step 1: Vectorize the query. Encode the user question as a dense vector while keeping the original text for later keyword-based retrieval and reranking.
Step 2: Map to entities. Run a similarity search between the query vector and the entity description vectors, and take the top candidates. This step usually applies an oversampling factor: fetch twice as many candidates and then truncate, which lowers the risk of missing relevant entities in a single nearest-neighbor search:
selected = map_query_to_entities(query=query,vector_store=entity_embeddings,embedder=embedder,top_k=10,oversample_scaler=2,)
Step 3: Build conversation history. In multi-turn scenarios, compress prior questions and answers into context snippets. This is constrained by both a maximum turn count and a token budget so that history does not crowd out the space reserved for evidence.
Step 4: Retrieve community reports. From each matched entity, look up the community it belongs to and pull the corresponding community summary to provide macro-level background for the answer.
Step 5: Retrieve relationships and claims. Expand along edges to retrieve the explicit relationships between entities and the factual statements that carry source annotations. This part determines how logically coherent the answer is.
Step 6: Retrieve source text chunks. Use the source identifiers of entities and relationships to trace back to the original text blocks, which serve as the final verifiable evidence.
4. Key Points for Organizing the Generation Stage
The key in the generation stage is not to “stuff content in” but to organize it in layers: global background first, then the backbone formed by relationships and claims, and finally the source evidence. Context must be concatenated with fixed separators and trimmed to fit the token limit. The trimming priority should be source evidence first, then community summaries, then conversation history. The prompt should explicitly require the model to answer only from the provided material and to state clearly when evidence is insufficient, which helps suppress hallucination.
5. Engineering Implementation and Evaluation
Index construction can store intermediate artifacts in object storage, run batch jobs on a container platform, and use a message queue to trigger incremental updates. Logging, monitoring, and alerting track recall hit rate and response latency. To reduce the cost of building everything in-house, you can evaluate general-purpose vector search, graph database, and LLM services on cloud platforms such as Baidu AI Cloud, selecting by general service category.
Evaluation is best split into three layers:
- Retrieval layer: entity hit rate and evidence coverage.
- Generation layer: answer faithfulness and completeness.
- End-to-end: summarization quality for global questions and localization accuracy for local questions.
Before launch, prepare three evaluation sets covering global, local, and multi-turn question types. Relying only on single-turn factual questions leads to overly optimistic conclusions.
