What is community detection?
Community detection identifies groups of entities that are densely connected to each other but sparsely connected to entities in other groups. In GraphRAG, this reveals:- Thematic clusters: Groups of entities discussing related topics
- Organizational structure: How information in your dataset is naturally organized
- Multiple granularities: From broad themes to specific subtopics
- Navigation pathways: How to traverse from global to local information
In a knowledge graph visualization, each circle represents an entity sized by its degree (number of connections), and colors represent different community memberships.
The Leiden algorithm
GraphRAG uses the Leiden algorithm, a state-of-the-art community detection method that improves upon the Louvain algorithm.Why Leiden?
Quality
Scalability
Hierarchical
Deterministic
Algorithm overview
Graph preparation
- Nodes: Entities from the knowledge graph
- Edges: Relationships between entities
- Weights: Relationship strength (based on frequency and context)
Optional LCC filtering
Initial clustering
- High edge density within the community
- Low edge density between communities
Recursive refinement
- Apply Leiden to create level 0 (leaf communities)
- If any community exceeds max_cluster_size, subdivide it
- Repeat until all leaf communities are below threshold
- Create parent communities by aggregating children
Hierarchical Leiden implementation
GraphRAG uses a custom hierarchical implementation built on thegraspologic_native library:
Key parameters
- max_cluster_size
- use_lcc
- seed
- Advanced parameters
- Smaller values (5-10): More hierarchy levels, finer granularity, more detailed reports
- Larger values (20-50): Fewer levels, broader communities, less detailed reports
- Smaller communities → more community reports → higher LLM costs
- Larger communities → broader summaries → may miss nuances
- Default of 10 balances detail with cost
Community hierarchy structure
The hierarchical clustering produces a tree structure of communities.Hierarchy levels
Level 0: Leaf communities
Level 0: Leaf communities
- Maximum max_cluster_size entities per community
- Most detailed, specific topics
- Highest number of communities
- No children, only parents
- Community 0: [“Microsoft”, “Azure”, “Cloud Computing”]
- Community 1: [“Python”, “Pandas”, “NumPy”]
- Community 2: [“GraphRAG”, “Knowledge Graph”, “RAG”]
Level 1-N: Intermediate levels
Level 1-N: Intermediate levels
- Aggregate multiple child communities
- Broader thematic groupings
- Both parent and children relationships
- Fewer communities than level 0
- Community 10: Aggregates communities 0, 1, 2
- Topic: “Technology and Software”
Top level: Root communities
Top level: Root communities
- 1-5 communities typically
- Dataset-wide themes
- No parents, only children
- Most abstract summaries
- Community 100: All technology-related entities
- Community 101: All business-related entities
Community data model
Each community in the hierarchy contains:Relationship aggregation
Communities include only intra-community relationships—edges where both source and target are in the same community:Community summarization
After communities are detected, LLM-generated summaries make them human-readable and useful for retrieval.Report generation process
Gather community context
- Entity titles, types, and descriptions
- Relationship descriptions
- Covariate/claim information (if available)
- Text unit excerpts
- Child community summaries (for non-leaf communities)
Structure the prompt
- Community entities and their roles
- Key relationships and connections
- Important claims or facts
- Instructions for report structure
Generate report
- Title: Descriptive name for the community
- Summary: Executive overview of the community
- Key entities: Most important entities and their significance
- Findings: Main insights and themes
- Rating: Importance score (1-10)
Store and embed
- Stored in the community_reports table
- Embedded for semantic search in global queries
- Linked to their community metadata
Bottom-up summarization
- Leaf communities: Summarize raw entities and relationships
- Parent communities: Summarize child community reports (already condensed)
- Coherence: Each level builds on previous summaries
- Efficiency: Parent reports don’t need to re-process all raw entities
Configuration
Using communities in retrieval
Communities enable different search strategies:- Global search
- Local search
- DRIFT search
- Select hierarchy level: Choose level based on desired granularity
- Retrieve reports: Get all community reports at that level
- Map step: Generate intermediate answers from each report
- Reduce step: Aggregate intermediate answers into final response
- Root level: Broad, high-level themes (faster, less detailed)
- Mid level: Balanced breadth and depth
- Leaf level: Most detailed (slower, more comprehensive)
Analyzing community structure
Metrics to monitor
Number of levels
Communities per level
Entity distribution
- Communities with very few entities
- Unbalanced distribution
- Isolated entities
Coverage
Quality indicators
Coherent communities
Coherent communities
- Semantically related entities
- Dense internal connections
- Clear thematic focus
- Meaningful summaries
Hierarchy coherence
Hierarchy coherence
- Progressive abstraction from leaf to root
- Parent summaries that meaningfully aggregate children
- Clear thematic progression
Graph connectivity
Graph connectivity
- Most entities in the largest connected component
- Few isolated nodes
- Balanced degree distribution
Best practices
Start with defaults
Analyze results
- Number of communities at each level
- Sample community reports for coherence
- Entity distribution
- Hierarchy depth
Tune if needed
- Too broad: Decrease max_cluster_size for finer granularity
- Too fragmented: Increase max_cluster_size for broader communities
- Cost concerns: Larger clusters = fewer reports = lower cost
Consider use case