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:
<node pattern> ::=
"(" [ <node variable declaration> ]
[ <label expression> ]
[ <property specification> | <where clause> ] ")"
The simplest empty node pattern matches any node in the graph:
()
To match Person
nodes and bind them to the variable n
:
(n:Person)
To match nodes whose properties fullname
and age
with specific values:
({fullname: "John Doe", age: 30})
To match Person
nodes where the property age
is greater than 30, and bind these nodes to the variable n
:
(n:Person WHERE n.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 (pointing left/incoming, pointing right/outgoing, or any direction). A full edge pattern is composed of three optional successive parts apart from the direction indication:
<full edge pattern> ::=
<full edge pointing left> | <full edge pointing right> | <full edge any direction>
<full edge pointing left> ::=
"<-[" <edge pattern filter> "]-"
<full edge pointing right> ::=
"-[" <edge pattern filter> "]->"
<full edge any direction> ::=
"-[" <edge pattern filter> "]-"
<edge pattern filter> ::=
[ <edge variable declaration> ]
[ <label expression> ]
[ <property specification> | <where clause> ]
To match all edges in the graph and bind them to the variable e
:
()-[e]->()
To match outgoing WorkingIn
edges 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 e.score < 2]-()
Abbreviated Edge Pattern
An abbreviated edge pattern only contains edge direction (pointing left/incoming, pointing right/outgoing, or any direction) and does not support variable declaration and pattern filtering.
<abbreviated edge pattern> ::= "<-" | "->" | "-"
To match nodes that User
nodes can reach through an outgoing edge:
(:User)->(n)
To match all one-step paths in the graph:
p = ()->()
Syntactic Elements
Element Variable Declaration
Node variables and edge variables are collectively referred to as element variables.
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 Person
nodes:
MATCH (n:Person)
RETURN n.name
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 (schemas) and returns true when it matches the label for a node or an edge.
To match Movie
nodes:
(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 Movie
or Country
nodes:
(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:%)
Property Specification
Property specification encloses property key-value pairs in a pair of curly braces {}
inside a node or full edge pattern. This applies joint equalities to filter nodes or edges based on the values of their properties.
To match nodes whose properties type
and level
with specific values:
(n {type: "Gold", level: 5})
This is equivalent to the following using the WHERE
clause:
(n WHERE n.type = "Gold" AND n.level = 5)
WHERE Clause
You can use the WHERE
clause within a node or full edge pattern to apply conditions on properties. It offers more filtering conditions compared to the property specification, which only allows joint equalities based on key-value pairs.
To match Card
nodes where the property level
is greater than 3:
(c:Card WHERE c.level > 3)
To filter edges that have properties amount
and currency
, and ensure their values meet specified conditions:
(:Card)-[t WHERE t.amount > 10000 AND t.currency = 'USD']->()
This query throws a syntax error because amount
is used as if it were a variable. To specify a property in the WHERE
clause, you must use the dot operator .
to reference it, like (n:Card WHERE n.amount > 100)
.
MATCH p = (:Card WHERE amount > 100)-[]->()
RETURN p
This query throws a syntax error because you cannot use property specification and WHERE
clause together in an element pattern. Instead, you may write (n:Paper WHERE n.author = "Alex" AND n.score > 5)
.
MATCH (n:Paper {author: "Alex"} WHERE n.score > 5)
RETURN n