Overview
A path pattern is to match paths in the graph. It is composed of three successive parts:
- Path Variable Declaration - Optional
- Path Pattern Prefix - Optional
- Path Pattern Expression - The core of a path pattern, which defines the sequence of nodes and edges that form the path. A path pattern expression can be:
- A single path term.
- Multiple path terms combined using union or multiset alternation.
<path pattern> ::=
[ <path variable declaration> ] [ <path pattern prefix> ] <path pattern expression>
<path pattern expression> ::=
<path term> | <path term union> | <path term multiset alteration>
Path Term
A path term consists of either a singleton or a concatenation of element patterns, parenthesized path patterns, and quantified path patterns. It follows the topological rules of a path: it must start and end with a node and alternate between nodes and edges.
<path term> ::=
<path factor> [ <path factor>... ]
<path factor> ::=
<element pattern> | <parenthesized path pattern> | <quantified path pattern>
Element Pattern
A path term can be built by concatenating node and edge patterns recursively. For example, this path term starts from node Brainy
, connecting to a Club
node through an outgoing Joins
edge:

(:User {name: 'Brainy'})-[:Joins]->(:Club)
You can keep on chaining edge and node patterns to create more complex path patterns. This path term describes that Brainy
and mochaeach
join the same club, with the Club
node bound to the variable c
:

(:User {name: 'Brainy'})-[:Joins]->(c:Club)<-[:Joins]-(:User {name: 'mochaeach'})
This path term reuses the variable a
to form a ring-like structure that starts and ends with the same Account
node:

(a:Account)-[:Owns]->(:Card)-[:Transfers]->(:Card)-[:Transfers]->(:Card)<-[:Owns]-(a)
Parenthesized Path Pattern
You can enclose a subpath or the entire path in a matching pair of parentheses ()
to form a parenthesized path pattern:
<parenthesized path pattern> ::=
"(" [ <path mode prefix> ] <path pattern expression> [ <where clause> ] ")"
Parenthesized path pattern currently can only be used with a quantifer to form a quantified path pattern.
Quantified Path Pattern
A quantifier can be affixed to an edge pattern or a parenthesized path pattern to form a quantified path pattern, constructing a path of variable length.
Path Term Union & Multiset Alternation
The Path Term Union & Multiset Alternation feature is not fully supported yet.
<path term union> ::=
<path term> "|" <path term> [ { "|" <path term> }... ]
<path term multiset alternation> ::=
<path term> "|+|" <path term> [ { "|+|" <path term> }... ]
Example Graph

CREATE GRAPH myGraph {
NODE User ({name string}),
NODE Club (),
EDGE Joins ()-[{}]->()
} PARTITION BY HASH(Crc32) SHARDS [1]
INSERT (rowlock:User {_id: 'U01', name: 'rowlock'}),
(lionbower:User {_id: 'U02', name: 'lionbower'}),
(mochaeach:User {_id: 'U03', name: 'mochaeach'}),
(c01:Club {_id: 'C01'}),
(c02:Club {_id: 'C02'}),
(rowlock)-[:Joins]->(c01),
(lionbower)-[:Joins]->(c01),
(lionbower)-[:Joins]->(c02),
(mochaeach)-[:Joins]->(c02)
Path term union combines path terms using the vertical bar |
. It merges records bound to the same variable while removing duplicates.
MATCH ({_id: "C01"})<-[:Joins]-(u:User) | ({_id: "C02"})<-[:Joins]-(u:User)
RETURN COLLECT_LIST(u.name) AS names
Result:
names |
---|
["rowlock", "lionbower", "mochaeach"] |
Path multiset alternation combines path terms using the multiset alternation operator |+|
. It merges records bound to the same variable while ensuring that duplicates are retained.
MATCH ({_id: "C01"})<-[:Joins]-(u:User) |+| ({_id: "C02"})<-[:Joins]-(u:User)
RETURN COLLECT_LIST(u.name) AS names
Result:
names |
---|
["rowlock", "lionbower", "lionbower", "mochaeach"] |
Note that each path term may only reference element variables decalred in the path term itself. For example, the following query throws syntax error:
MATCH ({code: "C01"})<-[]-(a) |
({code: "C02"})<-[]-(b WHERE a.name = b.name)
RETURN a, b
Path Pattern Prefix
There are two types of path pattern prefixes:
<path pattern prefix> ::=
<path mode> | <path search>
<path mode> ::=
"TRAIL" | "SIMPLE" | "ACYCLIC" | "WALK"
<path search> ::=
"ALL" [ <path mode> ]
| "ANY" [ <non negative integer> ] [ <path mode> ]
| "ALL SHORTEST" [ <path mode> ]
| "ANY SHORTEST" [ <path mode> ]
| "SHORTEST" <non negative integer> [ <path mode> ]
| "SHORTEST" [ <non negative integer> ] [ <path mode> ] < "GROUP" | "GROUPS" >
Example Graph

CREATE GRAPH myGraph {
NODE Card (),
EDGE Transfers ()-[{}]->()
} PARTITION BY HASH(Crc32) SHARDS [1]
INSERT (c01:Card {_id: 'C01'}),
(c02:Card {_id: 'C02'}),
(c03:Card {_id: 'C03'}),
(c04:Card {_id: 'C04'}),
(c01)-[:Transfers]->(c02),
(c01)-[:Transfers]->(c04),
(c02)-[:Transfers]->(c03),
(c03)-[:Transfers]->(c02),
(c03)-[:Transfers]->(c04)
Path Mode
Path modes (or restrictors) control how paths are traversed and whether nodes or edges can be revisited. A path mode may be placed either at the head of a path pattern expression, or at the head of a parenthesized path term.
Path Mode |
Description |
---|---|
TRAIL |
The default. Paths with repeated edges are not returned. |
ACYCLIC |
Paths with repeated nodes are not returned. |
SIMPLE |
Paths with repeated nodes are not returned unless these repeated nodes are the first and the last in the paths. |
WALK |
It is non-restrictive. |
The
ACYCLIC
,SIMPLE
, andWALK
are not yet supported.
The following two queries find 1 to 3 step paths with outgoing edges from C01
to C02
in the default TRAIL
mode and the ACYCLIC
mode respectively:
MATCH p = ({_id: 'C01'})->{1,3}({_id: 'C02'})
RETURN p
MATCH p = ACYCLIC ({_id: 'C01'})-[:Transfers]->{1,3}({_id: 'C02'})
RETURN p
Result: p

Path Search
Path searches (or selectors) select a finite set of matches from each partition[1]. A path search may only be placed at the head of a path pattern expression.
Path Search |
Description |
---|---|
ALL |
The default. Non-selective. |
ANY |
Selects one path in each partition. Non-deterministic. |
ANY k |
Selects k [2] paths in each partition. Non-deterministic. |
ALL SHORTEST |
Selects all shortest paths in each partition. |
ANY SHORTEST |
Selects one shortest path in each partition. Non-deterministic. |
SHORTEST k |
Selects k shortest paths in each partition. Non-deterministic. |
SHORTEST k GROUP |
Groups paths by length and sorts the groups in ascending order by length. Then selects all paths in the first k groups in each partition. Deterministic. |
[1] Partition: The paths matched by the path pattern are conceptually partitioned by distinct pairs of start and end nodes.
[2] k
: An non-negative integer. If fewer than k
paths exist, all are retained.
See Shortest Paths for more information on the shortest selectors.
The following two queries find 2 step paths between C01
and C03
, selecting ALL
(default) or ANY
paths from the results respectively:
MATCH p = ({_id: 'C01'})-()-({_id: 'C03'})
RETURN p
MATCH p = ANY ({_id: 'C01'})-()-({_id: 'C03'})
RETURN p
Result: p

Combined Usage
You can use either or both path mode and path search in a path pattern.
This query returns a single acyclic path starting C01
, passing through 1 to 3 outgoing Transfers
edges to another Card
node:
MATCH p = ANY ACYCLIC (c:Card {_id: 'C01'})-[:Transfers]->{1,3}(:Card)
RETURN p

The path term itself could find 5 paths. Firstly, the path including a repeated node C02
is removed by the path mode ACYCLIC
. The path search ANY
then randomly picks one path in each partition. In total, three paths are returned.
Path Variable
A path variable is declared at the start of a path pattern with =
, representing a path binding.
The variable p
is bound to paths connecting any two nodes through an outgoing Follows
edge:
MATCH p = ()-[:Follows]->()
RETURN p
Special Considerations
Node Pattern Juxtaposition
When two node patterns are concatenated consecutively, they bind to the same node with all filtering conditions considered conjunctively.
In this example, u1
and u2
implicitly merges to (u1|u2:User {name: "Claire"})
(The union operator |
here is for illustration only):
(u1:User)(u2 {name: "claire"})-[:Follows]-(u3)
Node pattern juxtaposition is currently only supported for quantified path patterns.
Edge Pattern Juxtaposition
Two consecutive edge patterns conceptually have an empty node pattern between them. For example,
(:User)-[]--[]-(u)
This path term implicitly extends to:
(:User)-[]-()-[]-(u)
A path term should not juxtapose a token that exposes a minus sign on the right (
]-
,<-
,-
) followed by a token that exposes a minus sign on the left (-[
,->
,-
), as this combination introduces the comment symbol--
. See Comments.