Path queries and pattern matching in NornicDB
NornicDB supports powerful graph traversal capabilities through Cypher queries. This guide covers path finding, pattern matching, and relationship navigation.
-- Find all nodes connected to a specific node
MATCH (start:Person {name: 'Alice'})-[r]->(connected)
RETURN connected, type(r) AS relationship
-- Find nodes connected by a specific relationship type
MATCH (p:Person)-[:KNOWS]->(friend:Person)
RETURN p.name, friend.name-- Find nodes within 1-3 hops
MATCH (start:Person {name: 'Alice'})-[*1..3]->(end)
RETURN DISTINCT end
-- Find all paths of any length (use with caution)
MATCH path = (a:Person)-[*]->(b:Person)
WHERE a.name = 'Alice' AND b.name = 'Bob'
RETURN path-- Find the shortest path between two nodes
MATCH path = shortestPath(
(a:Person {name: 'Alice'})-[*]-(b:Person {name: 'Bob'})
)
RETURN path, length(path) AS hops-- Find all equally short paths
MATCH path = allShortestPaths(
(a:Person {name: 'Alice'})-[*]-(b:Person {name: 'Bob'})
)
RETURN path-- Only traverse specific relationship types
MATCH path = (a:Person)-[:KNOWS|:WORKS_WITH*1..5]->(b:Person)
WHERE a.name = 'Alice'
RETURN path-- Only include nodes meeting criteria
MATCH path = (a:Person)-[*1..3]->(b:Person)
WHERE a.name = 'Alice'
AND ALL(node IN nodes(path) WHERE node.active = true)
RETURN path| Function | Description | Example |
|---|---|---|
nodes(path) |
Get all nodes in path | RETURN nodes(path) |
relationships(path) |
Get all relationships | RETURN relationships(path) |
length(path) |
Number of relationships | RETURN length(path) |
MATCH (me:Person {name: 'Alice'})-[:KNOWS]->(friend)-[:KNOWS]->(foaf)
WHERE foaf <> me
AND NOT (me)-[:KNOWS]->(foaf)
RETURN DISTINCT foaf.name AS suggestion-- Find all reports (direct and indirect)
MATCH path = (manager:Employee {name: 'CEO'})-[:MANAGES*]->(report)
RETURN report.name, length(path) AS level
ORDER BY level-- Find cycles in the graph
MATCH path = (n)-[*2..]->(n)
RETURN path LIMIT 10- Limit path length - Always specify max depth (
-[*1..5]->not-[*]->) - Use relationship types - Filter by type to reduce search space
- Start from selective nodes - Begin traversal from nodes with fewer connections
- Use LIMIT - Limit results for exploratory queries
- Cypher Queries - Complete Cypher guide
- Vector Search - Semantic search
- Complete Examples - Full working examples
Need more examples? → Complete Examples