4.3. Match

4.3.1. Related nodes
4.3.2. Outgoing relationships
4.3.3. Directed relationships and identifier
4.3.4. Match by relationship type
4.3.5. Match by relationship type and use an identifier
4.3.6. Multiple relationships
4.3.7. Variable length relationships
4.3.8. Optional relationship
4.3.9. Optional typed and named relationship
4.3.10. Complex matching

In the match part of a query, the pattern is described. The description of the pattern is made up of one or more paths, separated by commas.

All parts of the pattern must be directly or indirectly bound to a start point.

4.3.1. Related nodes

The symbol — means related to, without regard to type or direction.

Graph

Query

start n=(3) match (n)--(x) return x

All nodes related to A are returned

Result

 +--------------------+
 | x                  |
 +--------------------+
 | Node[4]{name->"B"} |
 | Node[1]{name->"D"} |
 | Node[5]{name->"C"} |
 +--------------------+
 3 rows, 2 ms

4.3.2. Outgoing relationships

When the direction of a relationship is interesting, it is shown by using --> or <--, like this:

Graph

Query

start n=(3) match (n)-->(x) return x

All nodes that A has outgoing relationships to.

Result

 +--------------------+
 | x                  |
 +--------------------+
 | Node[4]{name->"B"} |
 | Node[5]{name->"C"} |
 +--------------------+
 2 rows, 2 ms

4.3.3. Directed relationships and identifier

If an identifier is needed, either for filtering on properties of the relationship, or to return the relationship, this is how you introduce the identifier.

Graph

Query

start n=(3) match (n)-[r]->() return r

All outgoing relationships from node A.

Result

 +---------------+
 | r             |
 +---------------+
 | :KNOWS[0] {}  |
 | :BLOCKS[1] {} |
 +---------------+
 2 rows, 1 ms

4.3.4. Match by relationship type

When you know the relationship type you want to match on, you can specify it by using a colon.

Graph

Query

start n=(3) match (n)-[:BLOCKS]->(x) return x

All nodes that are BLOCKed by A.

Result

 +--------------------+
 | x                  |
 +--------------------+
 | Node[5]{name->"C"} |
 +--------------------+
 1 rows, 1 ms

4.3.5. Match by relationship type and use an identifier

If you both want to introduce an identifier to hold the relationship, and specify the relationship type you want, just add them both, like this.

Graph

Query

start n=(3) match (n)-[r:BLOCKS]->() return r

All BLOCK relationship going out from A.

Result

 +---------------+
 | r             |
 +---------------+
 | :BLOCKS[1] {} |
 +---------------+
 1 rows, 1 ms

4.3.6. Multiple relationships

Relationships can be expressed by using multiple statements in the form of ()--(), or they can be stringed together, like this:

Graph

Query

start a=(3) match (a)-[:KNOWS]->(b)-[:KNOWS]->(c) return a,b,c

The three nodes in the path.

Result

 +--------------------------------------------------------------+
 | a                  | b                  | c                  |
 +--------------------------------------------------------------+
 | Node[3]{name->"A"} | Node[4]{name->"B"} | Node[2]{name->"E"} |
 +--------------------------------------------------------------+
 1 rows, 2 ms

4.3.7. Variable length relationships

Nodes that are variable number of relationship→node hops can be found using the -[:TYPE^minHops..maxHops]→.

Graph

Query

start a=(3), x=(2, 4) match a-[:KNOWS^1..3]->x return a,x

The three nodes in the path.

Result

 +-----------------------------------------+
 | a                  | x                  |
 +-----------------------------------------+
 | Node[3]{name->"A"} | Node[2]{name->"E"} |
 | Node[3]{name->"A"} | Node[4]{name->"B"} |
 +-----------------------------------------+
 2 rows, 2 ms

4.3.8. Optional relationship

If a relationship is optional, it can be marked with a question mark. This similar to how a SQL outer join works, if the relationship is there, it is returned. If it’s not, null is returned in it’s place. Remember that anything hanging of an optional relation, is in turn optional, unless it is connected with a bound node some other path.

Graph

Query

start a=(2) match a-[?]->x return a,x

A node, and null, since the node has no relationships.

Result

 +-----------------------------+
 | a                  | x      |
 +-----------------------------+
 | Node[2]{name->"E"} | <null> |
 +-----------------------------+
 1 rows, 1 ms

4.3.9. Optional typed and named relationship

Just as with a normal relationship, you can decide which identifier it goes into, and what relationship type you need.

Graph

Query

start a=(3) match a-[r?:LOVES]->() return a,r

A node, and null, since the node has no relationships.

Result

 +-----------------------------+
 | a                  | r      |
 +-----------------------------+
 | Node[3]{name->"A"} | <null> |
 +-----------------------------+
 1 rows, 1 ms

4.3.10. Complex matching

Using Cypher, you can also express more complex patterns to match on, like a diamond shape pattern.

Graph

Query

start a=(3)
match (a)-[:KNOWS]->(b)-[:KNOWS]->(c), (a)-[:BLOCKS]-(d)-[:KNOWS]-(c)
return a,b,c,d

The four nodes in the path.

Result

 +-----------------------------------------------------------------------------------+
 | a                  | b                  | c                  | d                  |
 +-----------------------------------------------------------------------------------+
 | Node[3]{name->"A"} | Node[4]{name->"B"} | Node[2]{name->"E"} | Node[5]{name->"C"} |
 +-----------------------------------------------------------------------------------+
 1 rows, 3 ms