Overview
Node patterns and edge patterns are conjunctively referred to as element patterns, they serve as building blocks for path patterns.
Node Patterns
A node pattern is to match nodes in the graph, represented using a pair of parentheses ()
. A node pattern is composed of three optional successive parts:
- Element Variable Declaration
- Label Expression
- Element Pattern Predicate:
<node pattern> ::=
"(" [ <element variable declaration> ]
[ <label expression> ]
[ <element pattern predicate> ] ")"
<element pattern predicate> ::=
<element property specification> | <element pattern where clause>
The simplest empty node pattern matches any node in the graph:
()
To match nodes labeled Person
and bind them to the variable n
:
(n:Person)
To match nodes whose properties fullname
and age
have specific values:
({fullname: "John Doe", age: 30})
To match nodes labeled Person
where the property age
is greater than 30, and bind these nodes to the variable n
:
(n:Person WHERE age > 30)
Edge Patterns
An edge pattern is to match edges in the graph, typically in conjunction with node patterns on both sides, effectively forming a path pattern. If a node pattern is not provided on either side of the edge pattern, an implicit empty node pattern is assumed.
An edge pattern is either a full edge pattern or an abbreviated edge pattern.
<edge pattern> ::= <full edge pattern> | <abbreviated edge pattern>
Full Edge Pattern
A full edge pattern is represented using a pair of suqare brackets []
and the edge direction. Full edge patterns include:
- Full edge pointing left:
<-[]-
- Full edge pointing right:
-[]->
- Full edge any direction:
-[]-
A full edge pattern is composed of three optional successive parts:
- Element Variable Declaration
- Label Expression
- Element Pattern Predicate:
<full edge pattern> ::=
<full edge pointing left> | <full edge pointing right> | <full edge any direction>
<full edge pointing left> ::=
"<-[" [ <element variable declaration> ] [ <label expression> ] [ <element pattern predicate> ] "]-"
<full edge pointing right> ::=
"-[" [ <element variable declaration> ] [ <label expression> ] [ <element pattern predicate> ] "]->"
<full edge any direction> ::=
"-[" [ <element variable declaration> ] [ <label expression> ] [ <element pattern predicate> ] "]-"
<element pattern predicate> ::=
<element property specification> | <element pattern where clause>
To match all edges in the graph and bind them to the variable e
:
()-[e]-()
To match outgoing edges labeled WorkingIn
whose property role
has a specific value, and bind them to the variable e
:
()-[e:WorkingIn {role: "Manager"}]->()
To match incoming edges where the property score
is less than 2, and bind them to the variable e
:
<-[e WHERE score < 2]-
The above individual edge pattern implicitly extends to a path pattern as follows:
()<-[e WHERE score < 2]-()
Abbreviated Edge Pattern
An abbreviated edge pattern only contains edge direction thus does not support variable declaration and pattern filtering. Abbreviated edge patterns include:
- Abbreviated edge pointing left:
<-
- Abbreviated edge pointing right:
->
- Abbreviated edge any direction:
-
<abbreviated edge pattern> ::= "<-" | "->" | "-"
To match nodes that nodes labeled User
can reach through an outgoing edge:
(:User)->(n)
To match all one-step paths in the graph:
p = -
The above individual edge pattern implicitly extends to a path pattern as follows:
p = ()-()
Element Variable
Node variables and edge variables are collectively referred to as element variables.
Node Variable
A node variable is declared in a node pattern, placed before any label or property filters. The value of a node variable represents a list of bound nodes.
The variable n
is bound to nodes labeled Person
:
MATCH (n:Person)
RETURN n.name
Edge Variable
An edge variable is declared in a full edge pattern, placed before any label or property filters. The value of an edge variable represents a list of bound edges.
The variable e
is bound to edges labeled Follows
:
MATCH ()-[e:Follows]->()
RETURN e
Label Expression
A label expression is a boolean predicate that starts with a colon :
or the keyword IS
the within a node or full edge pattern. It specifies one or more labels and returns true when it matches the label for a node or an edge.
To match nodes labeled Movie
:
(m:Movie)
This is equivalent to:
(m IS Movie)
The label expression can combine multiple labels using the following operators:
Operator | Description | Precedence |
---|---|---|
() |
Grouping | 1 (Highest) |
! |
Negation (NOT) | 2 |
& |
Conjunction (AND) | 3 |
| |
Disjunction (OR) | 4 |
To match nodes labeled Movie
or Country
:
(n:Movie|Country)
To match edges labeled other than LIVING_IN
:
()-[e:!LIVING_IN]-()
To match nodes labeled other than Movie
and Country
:
(n:!(Movie&Country))
In Ultipa, each node or edge has one and only one label. Therefore, this will never match a node:
(n:Movie&Country)
There is also a wildcard symbol %
matching any
label:
(n:%)
Element Property Specification
Property key-value pairs can be enclosed in a pair of curly braces {}
inside a node or full edge pattern. This allows applying joint equalities to filter nodes or edges based on the values of their properties.
To match nodes whose properties type
and level
have specific values:
(n {type: "Gold", level: 5})
This is equivalent to the following using the element pattern WHERE
clause:
(n WHERE type = "Gold" AND level = 5)