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.
The symbol — means related to, without regard to type or direction.
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, 1 ms
When the direction of a relationship is interesting, it is shown by using -->
or <--
, like this:
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, 1 ms
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.
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
When you know the relationship type you want to match on, you can specify it by using a colon.
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
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.
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
Relationships can be expressed by using multiple statements in the form of ()--(), or they can be stringed together, like this:
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
Using Cypher, you can also express more complex patterns to match on, like a diamond shape pattern.
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, 1 ms
Copyright © 2011 Neo Technology