Back to series
· 17 min read

GraphRAG and Knowledge Graphs: Complete Technical Principles from Relationship Modeling to Graph-Enhanced Retrieval


1. Why Do We Need GraphRAG?

Traditional RAG (Retrieval-Augmented Generation) mainly relies on text chunking and vector similarity retrieval. It is good at finding semantically relevant text fragments, but it has clear limitations when dealing with complex relationships, multi-hop reasoning, and global questions.

For example, an enterprise knowledge base contains the following information:

  • Company A acquired Company B.
  • Company B holds some shares in Company C.
  • Company C is a core supplier for Project D.
  • Project D is the responsibility of Department E.

If a user asks:

Through which enterprises does Company A indirectly influence the supply chain of Project D?

Traditional vector retrieval may find the document about Company A acquiring Company B, and may also find the relationship between Company C and Project D, but it may not necessarily connect these pieces of information together.

This is because vector retrieval mainly measures semantic similarity of text, and does not inherently understand the topological relationships between entities.

The core idea of GraphRAG is:

Transform unstructured documents into a knowledge graph composed of entities, relationships, and facts, then use the graph structure to enhance retrieval and reasoning.

It focuses not only on whether a piece of text is similar to the question, but also on:

  • Which entities are involved?
  • What relationships exist between the entities?
  • Can the relationships form multi-hop paths?
  • Do multiple documents describe the same entity?
  • Are there larger-scale connections among local facts?
  • Does the current question require local relationship queries or global structural analysis?

2. What Is a Knowledge Graph?

A knowledge graph (KG) is a data model that organizes knowledge in a graph structure.

It uses nodes to represent entities, edges to represent relationships between entities, and attributes to describe specific information about entities and relationships.

2.1 Basic Components of a Knowledge Graph

A knowledge graph usually contains four core concepts:

Concept Meaning Example
Entity An object in the real world Company, person, product
Relation An association between entities Acquire, employ, produce
Attribute Descriptive information about an entity or relation Founding date, amount
Triple Represents a structured fact Company A—acquired—Company B

The most common basic representation of a knowledge graph is a triple:

(h,r,t)

Where:

  • h: Head, the head entity.
  • r: Relation, the relationship.
  • t: Tail, the tail entity.

For example:

(\text{Company A},\text{acquired},\text{Company B})

This triple indicates that Company A acquired Company B.

A set of knowledge triples can form a graph:

flowchart LR
    A["Company A"]
    B["Company B"]
    C["Company C"]
    D["Project D"]
    E["Department E"]

    A -->|"acquired"| B
    B -->|"holds shares"| C
    C -->|"supplies products"| D
    E -->|"responsible for"| D

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#DCEFE5,stroke:#83A995,color:#315D49
    style C fill:#EAE8FA,stroke:#A49BD5,color:#51477D
    style D fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
    style E fill:#F5F8F4,stroke:#B5CBBB,color:#40594A

This graph stores not only entity information but also the relationships between entities.

Compared with ordinary text, a graph structure can directly express the connection paths among multiple entities.

2.2 Differences Between Knowledge Graphs and Ordinary Databases

Both knowledge graphs and relational databases can store structured data, but their ways of organizing and querying it differ.

Comparison Dimension Relational Database Knowledge Graph
Core structure Tables, rows, columns Nodes, edges, attributes
Data association Foreign keys, JOIN Graph relationships
Relationship expression Defined through table structure Expressed directly through edges
Multi-hop query Multiple JOINs Graph traversal
Schema extension Usually requires adjusting table structure Can add entity types and relation types
Typical queries Aggregation, filtering, associative queries Path queries, relationship discovery, graph traversal

It should be noted that a knowledge graph is not a replacement for a relational database.

Relational databases are very suitable for transaction processing, structured data management, and precise aggregation; knowledge graphs are better suited for expressing complex relationships, association paths, and cross-entity knowledge.

The two can also work together.

2.3 Common Data Models of Knowledge Graphs

A knowledge graph does not have only one storage model.

Common data models include:

1. RDF (Resource Description Framework)

RDF uses subject, predicate, and object to express facts, emphasizing resource identification, semantic interoperability, and standardized description.

For example:

:CompanyA :acquired :CompanyB .
:CompanyB :owns :CompanyC .
:CompanyC :supplies :ProjectD .

RDF is commonly used in scenarios such as the Semantic Web, ontology modeling, and cross-system knowledge sharing.

2. Property Graph

A property graph consists of nodes, edges, and properties.

Both nodes and edges can have multiple properties.

For example:

Node:
CompanyA {
    name: "Company A",
    industry: "Technology"
}

Relationship:
CompanyA -[:ACQUIRED {
    date: "2025-01-01",
    amount: 1000000
}]-> CompanyB

Property graphs are very common in graph databases, enterprise knowledge networks, and relationship analysis.

3. Ontology

An ontology is used to define concepts, categories, relationships, and constraints in a domain.

For example:

  • A company belongs to an organization.
  • A person is a natural person.
  • Acquisition is a type of corporate transaction relationship.
  • An equity-holding relationship has a shareholding percentage and an effective date.

Ontology addresses the question of how knowledge is defined, classified, and constrained.

A knowledge graph, by contrast, carries specific entities and facts on top of these concept and relationship definitions.

3. Differences Between Traditional RAG and GraphRAG

Traditional RAG mainly organizes retrieval around text chunks, whereas GraphRAG introduces entities, relationships, and graph structures.

3.1 How Traditional RAG Works

Traditional RAG usually includes the following steps:

  1. Document parsing.
  2. Text chunking.
  3. Embedding vectorization.
  4. Vector indexing.
  5. Retrieve relevant text based on the user's question.
  6. Pass the retrieval results to a large language model to generate an answer.
flowchart TD
    A["Original document"]
    B["Document parsing"]
    C["Text chunking"]
    D["Embedding vectorization"]
    E["Vector database"]
    F["User question"]
    G["Question vectorization"]
    H["Similarity search"]
    I["Retrieved text chunks"]
    J["Build context"]
    K["Large language model"]
    L["Generate answer"]

    A --> B --> C --> D --> E
    F --> G --> H
    E --> H
    H --> I --> J --> K --> L

    style A fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
    style B fill:#EAF3EC,stroke:#83A995,color:#315D49
    style C fill:#EAF3EC,stroke:#83A995,color:#315D49
    style D fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style E fill:#EAE8FA,stroke:#A49BD5,color:#51477D
    style F fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
    style G fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style H fill:#EAF3EC,stroke:#83A995,color:#315D49
    style I fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
    style J fill:#EAF3EC,stroke:#83A995,color:#315D49
    style K fill:#EAE8FA,stroke:#A49BD5,color:#51477D
    style L fill:#DCEFE5,stroke:#83A995,color:#315D49

The core retrieval object of traditional RAG is text chunks.

Its advantages are simple implementation and strong adaptability, and it can handle large amounts of unstructured documents.

However, it also has several typical problems:

  • Relationships between text chunks may not be retrievable.
  • The same entity may use different names in different documents.
  • Multi-hop relationships need to be pieced together by the model itself.
  • Vector similarity cannot directly represent the logical relationships between facts.
  • It has limited support for global questions that span documents and entities.

3.2 How GraphRAG Works

GraphRAG adds knowledge graph construction and graph-structure retrieval capabilities on top of traditional RAG.

A typical process includes:

  1. Document parsing.
  2. Entity recognition.
  3. Relationship extraction.
  4. Entity disambiguation and merging.
  5. Knowledge graph construction.
  6. Community detection and summary generation (used in some GraphRAG architectures).
  7. Perform graph retrieval, vector retrieval, or hybrid retrieval based on the question.
  8. Integrate graph relationships, original text evidence, and summaries.
  9. Pass to a large language model to generate an answer.
flowchart TD
    A["Raw documents"]
    B["Document parsing"]
    C["Entity recognition"]
    D["Relation extraction"]
    E["Entity disambiguation and fusion"]
    F["Knowledge graph construction"]
    G["Community detection"]
    H["Community summarization"]
    I["Graph database and index"]

    Q["User question"]
    R["Question understanding"]
    S["Entity recognition and query planning"]
    T["Graph retrieval / vector retrieval"]
    U["Relation paths and textual evidence"]
    V["Context construction"]
    W["Large language model"]
    X["Answer generation"]

    A --> B --> C --> D --> E --> F
    F --> G --> H
    F --> I
    H --> I
    Q --> R --> S --> T
    I --> T
    T --> U --> V --> W --> X

    style A fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
    style B fill:#EAF3EC,stroke:#83A995,color:#315D49
    style C fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style D fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style E fill:#EAE8FA,stroke:#A49BD5,color:#51477D
    style F fill:#DCEFE5,stroke:#83A995,color:#315D49
    style G fill:#EAF3EC,stroke:#83A995,color:#315D49
    style H fill:#EAF3EC,stroke:#83A995,color:#315D49
    style I fill:#EAE8FA,stroke:#A49BD5,color:#51477D
    style Q fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
    style R fill:#EAF3EC,stroke:#83A995,color:#315D49
    style S fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style T fill:#EAF3EC,stroke:#83A995,color:#315D49
    style U fill:#EAE8FA,stroke:#A49BD5,color:#51477D
    style V fill:#EAF3EC,stroke:#83A995,color:#315D49
    style W fill:#EAE8FA,stroke:#A49BD5,color:#51477D
    style X fill:#DCEFE5,stroke:#83A995,color:#315D49

It should be emphasized: not all GraphRAG systems must use community detection, community summarization, or the same retrieval method.

GraphRAG is a class of graph-enhanced retrieval architectures, not a single fixed algorithm.

3.3 Key differences

Comparison dimension Traditional RAG GraphRAG
Core data Text snippets, vectors Entities, relations, text, vectors
Retrieval targets Semantically relevant text Entities, relations, paths, text
Association modeling Implicit associations Explicit relationships
Multi-hop queries Relies on text recall and model reasoning Can find paths through graph traversal
Global analysis Usually weaker Can leverage communities and global summaries
Data construction cost Relatively low Usually high
Query complexity Relatively simple Requires query planning and graph retrieval
Typical scenarios Document QA, knowledge base retrieval Complex relationship analysis, cross-document reasoning

GraphRAG does not mean abandoning vector retrieval.

Practical systems usually combine:

  • Vector retrieval: discover semantically relevant content.
  • Keyword retrieval: locate explicit terms and entity names.
  • Graph retrieval: find relationship paths between entities.
  • Textual evidence retrieval: return to the original documents to verify facts.

4. Core Technologies of GraphRAG: Entity Recognition and Relation Extraction

The quality of a knowledge graph depends to a large extent on the ability to extract knowledge from raw documents.

In the data construction phase of GraphRAG, it is usually necessary to convert natural language into structured entities and relations.

4.1 Entity Recognition

Entity recognition is the identification of objects with clear semantics from text.

For example:

OpenAI launched ChatGPT in 2022, and the product is based on large language model technology.

The following can be identified:

Entity Type
OpenAI Organization
ChatGPT Product
2022 Time
Large language model Technology

Entity types need to be defined in line with the business domain.

Common types include:

  • Person
  • Organization
  • Company
  • Product
  • Technology
  • Location
  • Event
  • Time
  • Document

In general NLP, entity recognition is often referred to as NER (Named Entity Recognition).

But entity extraction in GraphRAG is usually not limited to traditional named entities; it may also include domain concepts, events, technical capabilities and abstract topics.

4.2 Relation Extraction

Relation extraction is used to identify the semantic relationship between two entities.

For example:

OpenAI launched ChatGPT in 2022.

We can extract:

{
  "head": "OpenAI",
  "relation": "launch",
  "tail": "ChatGPT"
}

A more complete relation can carry properties:

{
  "head": "OpenAI",
  "relation": "launch",
  "tail": "ChatGPT",
  "properties": {
    "year": 2022,
    "source": "product release record",
    "confidence": 0.96
  }
}

Relation types need to be managed uniformly; otherwise the same semantics may be extracted as several different relation types.

For example:

  • found
  • create
  • establish
  • founded

Without a relation normalization mechanism, the graph can end up containing a large number of duplicate, similar but inconsistent relation types.

4.3 Entity Disambiguation and Entity Resolution

Entity disambiguation addresses the question of whether different names point to the same entity.

For example:

  • OpenAI
  • OpenAI Company
  • OpenAI, Inc.

In a given context, these names may all point to the same organization.

Entity resolution goes a step further and determines whether different records should be merged into a single entity.

Common criteria include:

  1. Name similarity.
  2. Alias mapping.
  3. Entity type.
  4. Description information.
  5. External unique identifiers.
  6. Upstream and downstream relations.
  7. Time and business context.

Note that similar names do not mean the same entity.

For example:

  • Apple the company.
  • Apple the fruit.
  • Apple-branded products.

Merging based on string similarity alone easily leads to incorrect associations.

In production environments, stable entity IDs should be used wherever possible, and aliases, sources and the basis for merging should be retained.

4.4 The Complete Process of Knowledge Extraction

flowchart TD
    A["Input document"]
    B["Text preprocessing"]
    C["Entity recognition"]
    D["Relation extraction"]
    E["Attribute extraction"]
    F["Entity disambiguation"]
    G{"Matches an existing entity?"}
    H["Link to existing entity ID"]
    I["Create new entity"]
    J["Relation normalization"]
    K["Fact validation"]
    L{"Passes validation?"}
    M["Write to knowledge graph"]
    N["Route to manual review or quarantine"]

    A --> B --> C --> D --> E --> F --> G
    G -->|"Yes"| H
    G -->|"No"| I
    H --> J
    I --> J
    J --> K --> L
    L -->|"Yes"| M
    L -->|"No"| N

    style A fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
    style B fill:#EAF3EC,stroke:#83A995,color:#315D49
    style C fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style D fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style E fill:#EAF3EC,stroke:#83A995,color:#315D49
    style F fill:#EAE8FA,stroke:#A49BD5,color:#51477D
    style G fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
    style H fill:#EAF3EC,stroke:#83A995,color:#315D49
    style I fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style J fill:#EAE8FA,stroke:#A49BD5,color:#51477D
    style K fill:#EAF3EC,stroke:#83A995,color:#315D49
    style L fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
    style M fill:#DCEFE5,stroke:#83A995,color:#315D49
    style N fill:#F7EAE7,stroke:#C98D7C,color:#754438

A real system also needs to account for:

  • Repeated extraction from the same document.
  • Entity name changes.
  • Conflicting relations.
  • Facts becoming invalid.
  • Document retraction.
  • Extraction results that cannot be verified.
  • Invalid output formats from the large model.

In particular, a fact in the knowledge graph should not consist of just a triple; it should also retain source and time information.

Otherwise, when different documents contain contradictory descriptions, the system will struggle to determine which fact is trustworthy.

V. Building and Storing the Knowledge Graph

Once entity and relation extraction is complete, the structured knowledge must be organized into a queryable graph.

5.1 From Triples to Graph Structure

Suppose the following facts are extracted from multiple documents:

(Company A, acquires, Company B)
(Company B, holds shares in, Company C)
(Company C, supplies products to, Project D)
(Department E, is responsible for, Project D)

These triples can form the following knowledge graph:

flowchart LR
    A["Company A"]
    B["Company B"]
    C["Company C"]
    D["Project D"]
    E["Department E"]

    A -->|"acquires"| B
    B -->|"holds shares in"| C
    C -->|"supplies products to"| D
    E -->|"is responsible for"| D

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#DCEFE5,stroke:#83A995,color:#315D49
    style C fill:#EAE8FA,stroke:#A49BD5,color:#51477D
    style D fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
    style E fill:#F5F8F4,stroke:#B5CBBB,color:#40594A

At this point, each entity in the graph can be associated with multiple attributes, and each relation can likewise carry multiple attributes.

For example:

{
  "source": "Company A",
  "target": "Company B",
  "type": "acquisition",
  "properties": {
    "date": "2025-01-01",
    "source_document": "document_001",
    "confidence": 0.96
  }
}

Relations themselves also need to be managed, because relations in the real world may carry a time, an amount, a proportion, a status and a source.

5.2 Graph Databases

A graph database is a database built specifically for storing and querying graph-structured data.

Common products and technologies include:

  • Neo4j: property graph database that supports the Cypher query language.
  • NebulaGraph: distributed graph database.
  • Amazon Neptune: supports graph data models such as property graphs and RDF.
  • JanusGraph: distributed graph database framework for large-scale graph data.
  • RDF4J: Java framework for RDF data management and querying.

Graph databases typically need to address:

  • Node storage.
  • Edge storage.
  • Adjacency indexing.
  • Property indexing.
  • Graph traversal.
  • Query optimization.
  • Concurrent access.
  • Data consistency.

5.3 Cypher Query Example

Take Neo4j's property graph model as an example.

Create entities and relationships:

CREATE (a:Company {name: "A公司"})
CREATE (b:Company {name: "B公司"})
CREATE (c:Company {name: "C公司"})
CREATE (d:Project {name: "D项目"})

CREATE (a)-[:ACQUIRED]->(b)
CREATE (b)-[:OWNS]->(c)
CREATE (c)-[:SUPPLIES]->(d)

Query the D project associated with Company A:

MATCH
  (a:Company {name: "A公司"})
  -[:ACQUIRED]->()
  -[:OWNS]->(c:Company)
  -[:SUPPLIES]->(p:Project)
RETURN c, p

Query the two-hop neighbors of a specified entity:

MATCH
  (a:Company {name: "A公司"})-[r*1..2]-(n)
RETURN n, r

In actual production environments, the query depth should be set to a reasonable upper limit to avoid generating an excessively large traversal scope in highly connected graphs.

5.4 How Do Graph Databases and Vector Databases Work Together?

GraphRAG usually does not need to put all data in the same database.

A multi-store collaborative architecture can be used:

Storage System Primary Responsibility
Graph database Store entities, relationships, and graph structure
Vector database Store embeddings of texts and entities
Document store Store original documents and text chunks
Relational database Store business data, task status, and metadata
Search engine Support keyword and full-text search

These storage systems are linked through unified entity IDs, document IDs, and Chunk IDs.

For example, an entity in a knowledge graph can be associated with:

  • Entity description text.
  • Original documents.
  • Corresponding text chunks.
  • Vector index.
  • External business system ID.

In this way, the system can both find relationships through the graph structure and verify evidence through the original documents.

6. Core Retrieval Mechanisms of GraphRAG

The focus of GraphRAG retrieval is not simply replacing vector search with graph search, but selecting an appropriate retrieval method based on the question.

Common methods include:

  1. Entity retrieval.
  2. Neighbor retrieval.
  3. Multi-hop path retrieval.
  4. Vector retrieval.
  5. Hybrid retrieval.
  6. Community retrieval and global summary retrieval.

6.1 Entity Retrieval

First, identify key entities from the user's question.

For example:

Which companies has Company A acquired?

The system needs to identify:

  • Entity: Company A.
  • Entity type: Company.
  • Target relationship: ACQUIRED.
  • Query direction: start from Company A and find acquired companies.

Then locate the node in the graph by entity ID.

The core of entity retrieval is mapping entity names in natural language to canonical entities in the graph.

If entity mapping is wrong, even if subsequent graph traversal is executed correctly, it will still produce incorrect results.

6.2 Neighbor Retrieval

Neighbor retrieval is used to obtain nodes directly associated with a specified entity.

For example:

MATCH
  (a:Company {name: "A公司"})-[r]->(n)
RETURN n, r

If Company A has direct relationships with the three entities B, C, and D, then neighbor retrieval can return these associated entities.

Neighbor retrieval is suitable for:

  • Querying objects associated with an entity.
  • Obtaining upstream and downstream relationships of an entity.
  • Querying enterprise partners.
  • Querying a product's suppliers.
  • Querying a person's employment relationships.

The limitation of neighbor retrieval is that it can only obtain local information within a limited number of hops.

6.3 Multi-hop Path Retrieval

Multi-hop retrieval is used to find paths formed by multiple relationships connecting multiple entities.

For example:

A Company
  acquired
B Company
  holds shares
C Company
  supplies products
D Project

The user asks:

How does Company A affect the supply chain of Project D?

The system needs to find the association path between Company A and Project D.

flowchart LR
    A["Company A"]
    B["Company B"]
    C["Company C"]
    D["Project D"]

    A -->|"Acquires"| B
    B -->|"Holds shares"| C
    C -->|"Supplies products"| D

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#DCEFE5,stroke:#83A995,color:#315D49
    style C fill:#EAE8FA,stroke:#A49BD5,color:#51477D
    style D fill:#F7F4E8,stroke:#C7B77A,color:#675A2D

Multi-hop paths can represent:

  • Indirect equity relationships.
  • Organizational hierarchies.
  • Product supply chains.
  • Technology dependency chains.
  • Personnel collaboration networks.
  • Causal association clues between events.

Note that a graph path does not necessarily equal a real causal relationship.

For example, the fact that Company A acquires Company B and Company B holds shares in Company C does not, in all cases, directly imply that Company A has full control over Company C.

Graph traversal can only discover structural connections; the semantics, direction, timing, and business rules of relationships still require further judgment.

6.4 Path Queries and Path Constraints

Multi-hop retrieval cannot traverse all nodes unconditionally.

Practical queries need to define constraints:

  • Maximum number of hops.
  • Allowed relationship types.
  • Relationship direction.
  • Entity types.
  • Time range.
  • Relationship weights.
  • Path length.
  • Confidence threshold.

For example, when querying the path from Company A to Project D, you can restrict it to only traverse corporate acquisition, equity holding, and product supply relationships.

Without constraints, the graph may contain many irrelevant paths, causing result noise and rapidly increasing query costs.

6.5 Combining Graph Retrieval and Vector Retrieval

GraphRAG can combine vector retrieval and graph retrieval.

For example:

A user asks:

Which companies are related to Company A's core technology supply chain?

The system can:

  1. Use vector retrieval to find text related to core technologies and supply chains.
  2. Identify company entities from the text.
  3. Query the knowledge graph for relationships between companies.
  4. Expand and filter relationship paths.
  5. Return to the original documents to obtain evidence.
  6. Pass the results to the model to organize an answer.

This approach balances semantic understanding and structured relationship queries.

flowchart TD
    A["User question"]
    B["Question understanding"]
    C["Vector retrieval"]
    D["Entity recognition"]
    E["Graph database retrieval"]
    F["Multi-hop path expansion"]
    G["Original text evidence retrieval"]
    H["Result deduplication and ranking"]
    I["Context integration"]
    J["Large language model"]
    K["Answer generation"]

    A --> B
    B --> C
    B --> D
    C --> H
    D --> E --> F --> G --> H
    H --> I --> J --> K

    style A fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
    style B fill:#EAF3EC,stroke:#83A995,color:#315D49
    style C fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style D fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style E fill:#EAE8FA,stroke:#A49BD5,color:#51477D
    style F fill:#DCEFE5,stroke:#83A995,color:#315D49
    style G fill:#EAF3EC,stroke:#83A995,color:#315D49
    style H fill:#EAF3EC,stroke:#83A995,color:#315D49
    style I fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
    style J fill:#EAE8FA,stroke:#A49BD5,color:#51477D
    style K fill:#DCEFE5,stroke:#83A995,color:#315D49

7. Community Detection and Global Retrieval in GraphRAG

An important direction for GraphRAG is to use graph community structure to answer global questions across documents.

Traditional RAG usually recalls several text snippets based on the question.

But if a user asks:

What business directions are mainly discussed in this batch of internal enterprise documents? What connections exist between these directions?

This question does not necessarily correspond to a specific entity or text snippet.

It requires understanding the topic distribution, entity associations, and latent structure across the entire knowledge base.

GraphRAG can handle such questions through community detection and community summaries.

7.1 What Is a Graph Community?

A graph community is a group of nodes in a graph that are relatively tightly connected.

For example, an enterprise knowledge graph may contain the following associations:

  • A group of enterprises forms a collaboration network around cloud computing.
  • A group of products forms a technology dependency network around artificial intelligence.
  • A group of people forms a collaboration network around a certain project.
  • A group of documents forms a knowledge association network around a certain business topic.

These tightly connected entities may form different communities.

flowchart TB
    subgraph G1["Community A: Artificial Intelligence"]
        A1["Large Models"]
        A2["Embedding"]
        A3["RAG"]
        A4["Vector Database"]
        A1 --- A2
        A1 --- A3
        A3 --- A4
    end

    subgraph G2["Community B: Cloud Computing"]
        B1["Cloud Platform"]
        B2["Containers"]
        B3["Microservices"]
        B4["Distributed Storage"]
        B1 --- B2
        B1 --- B3
        B1 --- B4
    end

    A3 ---|"Technology deployment dependency"| B1

    style A1 fill:#DCEFE5,stroke:#83A995,color:#315D49
    style A2 fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style A3 fill:#EAE8FA,stroke:#A49BD5,color:#51477D
    style A4 fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
    style B1 fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
    style B2 fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B3 fill:#DCEFE5,stroke:#83A995,color:#315D49
    style B4 fill:#EAE8FA,stroke:#A49BD5,color:#51477D

Communities do not necessarily correspond to predefined business categories.

They are sets of associations computed from graph structure and algorithms.

An entity may also be related to multiple topics; the specific community partition depends on graph structure, edge weights, and algorithm parameters.

7.2 Community Detection Algorithms

Common community detection algorithms include:

1. Louvain Algorithm

Louvain is a community detection algorithm based on modularity optimization.

Modularity is used to measure the strength of connections within communities relative to the expected connections in a random network.

Its basic idea is:

  • Divide nodes into different communities.
  • Try moving nodes to adjacent communities.
  • Calculate the change in modularity before and after the move.
  • Keep adjustments that improve modularity.
  • Compress communities into supernodes.
  • Continue optimizing on the compressed graph.

2. Leiden Algorithm

Leiden is an improvement on Louvain.

It adds a community refinement process to improve community connectivity and avoid producing communities with insufficient internal connections.

3. Other Methods

There are also community detection methods based on label propagation, spectral clustering, and hierarchical clustering.

Different algorithms are suitable for different graph scales, structures, and business objectives.

7.3 What Is a Community Summary?

After communities are obtained through community detection, the entities, relationships, and related text within each community can be summarized.

For example:

Community Name: Artificial Intelligence Infrastructure

Main Entities:
- Large Language Models
- Embedding
- Vector Database
- RAG

Main Relationships:
- Large Language Models use Embedding to represent text
- RAG retrieves knowledge through a vector database
- Vector databases provide similarity search capabilities

Community Summary:
This community primarily focuses on infrastructure for large language model knowledge retrieval and applications,
with key areas including vector representation, vector retrieval, and retrieval-augmented generation.

Community summaries can serve as an important source of information for global retrieval.

Note that a summary is a compressed representation of graph structure and textual evidence, and is not equivalent to the original facts themselves.

Summaries may omit details, so when specific data, time, or relationships are involved, you should still return to the original documents and graph facts for verification.

7.4 Local Search and Global Search

In architectures such as Microsoft GraphRAG, common retrieval approaches can be divided into local search and global search.

Dimension Local Search Global Search
Retrieval Target Specific entities and their associations Themes and structure of the entire knowledge base
Primary Data Entities, relationships, related text Communities, community summaries
Typical Questions Which companies is a certain company related to? What are the main themes of the entire knowledge base?
Query Scope Local subgraph Multiple communities or global
Computational Cost Usually relatively low May be relatively high
Result Characteristics Rich in detail Themes and overview are more prominent

Local Search:

Suitable for answering questions about specific entities, relationships, and local associations.

Global Search:

Suitable for answering questions about global themes, cross-community synthesis, and knowledge base overviews.

Practical systems can also adopt retrieval strategies such as DRIFT Search that combine global discovery with local exploration; specific capabilities depend on the implementation version and configuration.

7.5 Basic Process of Global Retrieval

flowchart TD
    A["Knowledge Graph"]
    B["Community Detection"]
    C["Generate Community Summaries"]
    D["User Asks a Global Question"]
    E["Question Decomposition"]
    F["Select Relevant Community Summaries"]
    G["Generate Community-Level Answers in Parallel"]
    H["Aggregate Community Answers"]
    I["Integrate Global Results"]
    J["Generate Final Answer"]

    A --> B --> C
    D --> E --> F
    C --> F
    F --> G --> H --> I --> J

    style A fill:#EAE8FA,stroke:#A49BD5,color:#51477D
    style B fill:#EAF3EC,stroke:#83A995,color:#315D49
    style C fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style D fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
    style E fill:#EAF3EC,stroke:#83A995,color:#315D49
    style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style G fill:#EAE8FA,stroke:#A49BD5,color:#51477D
    style H fill:#EAF3EC,stroke:#83A995,color:#315D49
    style I fill:#DCEFE5,stroke:#83A995,color:#315D49
    style J fill:#DCEFE5,stroke:#83A995,color:#315D49

Global retrieval usually requires controlling the context size.

If there are many communities, you cannot simply put all community summaries into the prompt.

You can control token consumption through methods such as community filtering, hierarchical summarization, parallel processing, and result compression.

8. The Complete Engineering Architecture of GraphRAG

In production systems, GraphRAG is more than just a graph database plus a large language model.

It also requires modules such as document processing, knowledge extraction, index construction, query planning, evidence management, and result generation.

8.1 Offline Knowledge Construction Pipeline

The offline pipeline is responsible for converting raw documents into a queryable knowledge index.

flowchart TD
    A["Raw Documents"]
    B["Document Parsing"]
    C["Text Cleaning"]
    D["Text Chunking"]
    E["LLM Entity Extraction"]
    F["LLM Relation Extraction"]
    G["Entity Disambiguation"]
    H["Relation Normalization"]
    I["Fact Validation"]
    J["Knowledge Graph Storage"]
    K["Text Embedding"]
    L["Vector Index"]
    M["Community Detection"]
    N["Community Summaries"]
    O["Index Publishing"]

    A --> B --> C --> D
    D --> E --> F --> G --> H --> I --> J
    D --> K --> L
    J --> M --> N
    J --> O
    L --> O
    N --> O

    style A fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
    style B fill:#EAF3EC,stroke:#83A995,color:#315D49
    style C fill:#EAF3EC,stroke:#83A995,color:#315D49
    style D fill:#EAF3EC,stroke:#83A995,color:#315D49
    style E fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style G fill:#EAE8FA,stroke:#A49BD5,color:#51477D
    style H fill:#EAE8FA,stroke:#A49BD5,color:#51477D
    style I fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
    style J fill:#DCEFE5,stroke:#83A995,color:#315D49
    style K fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style L fill:#EAE8FA,stroke:#A49BD5,color:#51477D
    style M fill:#EAF3EC,stroke:#83A995,color:#315D49
    style N fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style O fill:#DCEFE5,stroke:#83A995,color:#315D49

One engineering detail needs attention here:

The knowledge graph, vector index, and community summaries must maintain consistent data versions.

If the knowledge graph has been updated but the vector index and community summaries still use an old version, the following problems may occur during queries:

  • The graph contains new entities, but the vector index cannot find the relevant text.
  • Graph relations are no longer valid, but community summaries still retain old descriptions.
  • The original document has been deleted, but related facts still exist in the graph.

Therefore, a unified data version or build batch identifier must be established for documents, entities, relations, and indexes.

8.2 Online Query Pipeline

The online pipeline is responsible for understanding the question, selecting a retrieval method, and generating an answer.