Relational Knowledge Retrieval

GraphRAG & Knowledge Graphs

Overcome the fundamental blind spots of vector search. Learn how Labeled Property Graphs (Neo4j, Memgraph), Cypher graph traversals, and Microsoft GraphRAG community detection unlock multi-hop reasoning and holistic sensemaking for AI agents.

1 Why Vector RAG Fails at Multi-Hop Reasoning

Problem 1: Chunk Fragmentation

Vector RAG chunks documents into isolated pieces. If Document A mentions "Alice is CEO of TechCorp" and Document B mentions "TechCorp acquired DataFlow", vector similarity cannot answer "Which company did Alice's firm acquire?" because the semantic bridge is severed.

Problem 2: Global Corpus Queries

Vector search cannot answer broad queries like "What are the primary recurring compliance risks across all 500 audit reports?" because top-k cosine similarity only fetches specific paragraphs, failing to synthesize holistic corpus patterns.

The Solution: GraphRAG

Extract Entities (Nodes) and Relationships (Edges) into a Knowledge Graph. Cluster interconnected communities using the Leiden algorithm, generating hierarchical summaries that enable both precise local traversals and high-level global sensemaking.

2 Labeled Property Graph & Cypher Fundamentals

Nodes & Labels

Entities in the domain (e.g. (:Person), (:Company), (:Incident)). Each node holds key-value properties such as {name: "Alice", role: "CEO"}.

Relationships & Edges

Directed connections between nodes: -[:MANAGES]->, -[:DEPENDS_ON]->. Relationships can also store properties (e.g. {since: 2024, confidence: 0.95}).

Index-Free Adjacency

Each node directly stores memory pointers to its adjacent neighbor nodes. Traversing 10 hops does not require table joins; it evaluates in O(k) time proportional to degrees, not overall database size.

Cypher Pattern Matching

Declarative graph query language: MATCH (p:Person)-[:WORKS_AT]->(c:Company) WHERE c.name = 'TechCorp' RETURN p.name.

3 Quickstart & Docker Compose (Neo4j 5.x)

Docker Compose Stack

# docker-compose.yml
services:
neo4j:
image: neo4j:5.23-community
container_name: neo4j-graphrag
ports:
- "7474:7474" # Browser UI
- "7687:7687" # Bolt Driver Protocol
environment:
- NEO4J_AUTH=neo4j/password123
- NEO4J_PLUGINS=["apoc"]

Python Client Installation

Install official Neo4j Bolt driver and LangChain graph utilities:

# Terminal
pip install neo4j langchain-community
# Connect via Bolt
from neo4j import GraphDatabase
driver = GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "password123"))

4 Production Python: Multi-Hop Agent Retrieval

Agent Graph Traversal Tool

# graphrag_retriever.py
def query_multihop_relationships(driver, start_entity: str, max_depth: int = 2):
query = """
MATCH path = (e:Entity {name: $start})-[:RELATION*1..""" + str(max_depth) + """]->(target:Entity)
RETURN [n in nodes(path) | n.name] AS entities,
[r in relationships(path) | type(r)] AS relations,
target.name AS destination
LIMIT 10
"""
with driver.session() as session:
result = session.run(query, start=start_entity)
return [record.data() for record in result]

5 FAQ

How does GraphRAG combine with Vector Databases? →
In a hybrid pipeline: Vector search first finds the top 3 entry nodes based on embedding similarity (e.g. finding "Kubernetes Cluster Alpha"). Then, the graph engine expands outward 1–2 hops along relationships (-[:DEPLOYED_ON]->, -[:DEPENDS_ON]->), assembling a complete relational context graph before prompt injection.
What is Microsoft GraphRAG's Leiden Community Detection? →
The Leiden algorithm partitions the entire knowledge graph into dense clusters of tightly connected concepts at multiple hierarchy levels (C0, C1, C2). An LLM pre-summarizes each community. When a high-level query arrives, the agent queries the community summaries rather than searching individual chunks, enabling global document understanding.