Skip to content

Latest commit

 

History

History
146 lines (103 loc) · 3.13 KB

File metadata and controls

146 lines (103 loc) · 3.13 KB

Graph Traversal Guide

Path queries and pattern matching in NornicDB


Overview

NornicDB supports powerful graph traversal capabilities through Cypher queries. This guide covers path finding, pattern matching, and relationship navigation.


Basic Pattern Matching

Find Connected Nodes

-- 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

Variable-Length Paths

-- 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

Path Finding

Shortest 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

All Shortest Paths

-- Find all equally short paths
MATCH path = allShortestPaths(
  (a:Person {name: 'Alice'})-[*]-(b:Person {name: 'Bob'})
)
RETURN path

Filtering Paths

Filter by Relationship Type

-- Only traverse specific relationship types
MATCH path = (a:Person)-[:KNOWS|:WORKS_WITH*1..5]->(b:Person)
WHERE a.name = 'Alice'
RETURN path

Filter by Node Properties

-- 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

Path Functions

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)

Common Patterns

Friend of Friend

MATCH (me:Person {name: 'Alice'})-[:KNOWS]->(friend)-[:KNOWS]->(foaf)
WHERE foaf <> me
  AND NOT (me)-[:KNOWS]->(foaf)
RETURN DISTINCT foaf.name AS suggestion

Hierarchical Queries

-- Find all reports (direct and indirect)
MATCH path = (manager:Employee {name: 'CEO'})-[:MANAGES*]->(report)
RETURN report.name, length(path) AS level
ORDER BY level

Circular References

-- Find cycles in the graph
MATCH path = (n)-[*2..]->(n)
RETURN path LIMIT 10

Performance Tips

  1. Limit path length - Always specify max depth (-[*1..5]-> not -[*]->)
  2. Use relationship types - Filter by type to reduce search space
  3. Start from selective nodes - Begin traversal from nodes with fewer connections
  4. Use LIMIT - Limit results for exploratory queries

Related Documentation


Need more examples?Complete Examples