Path Patterns
Variable-Length Paths
Use *min..max after the relationship type to traverse multiple hops.
-- 1 to 3 hops
MATCH (a:Person)-[:FRIEND*1..3]->(b:Person)
RETURN a.name, b.name
-- Exactly 2 hops
MATCH (a)-[:FRIEND*2]->(b)
RETURN a.name, b.name
-- Any length (use with caution on large graphs)
MATCH (a:Person {name: "Alice"})-[:FRIEND*]->(b)
RETURN b.nameQuantified Paths
Anvil supports GQL-style quantified path patterns for more precise hop control.
-- Exactly 3 hops
MATCH (a)-[:FRIEND]->{3}(b)
RETURN a.name, b.name
-- Between 2 and 5 hops
MATCH (a)-[:FRIEND]->{2,5}(b)
RETURN a.name, b.name
-- One or more hops
MATCH (a)-[:FRIEND]->+(b)
RETURN a.name, b.nameNamed Paths
-- Assign a path to a variable
MATCH p = (a:Person {name: "Alice"})-[:FRIEND*]->(b)
RETURN p, length(p), nodes(p), relationships(p)Shortest Path
-- Single shortest path
MATCH path = shortestPath(
(a:Person {name: "Alice"})-[:FRIEND*]-(b:Person {name: "Dave"})
)
RETURN path, length(path)
-- All shortest paths (same length)
MATCH p = ALL SHORTEST (a:Person {name: "Alice"})--+(b:Person {name: "Dave"})
RETURN pNegated Relationship Types
Exclude specific relationship types using the ! prefix.
-- Match any relationship except ACTED_IN MATCH (a:Person)-[:!ACTED_IN]->(b) RETURN a.name, type(b)
Path Functions
| Function | Description |
|---|---|
| length(path) | Number of relationships in the path |
| nodes(path) | List of all nodes in the path |
| relationships(path) | List of all relationships in the path |