Patterns
Cypher is a declarative query language that allows you to identify patterns in your data using an ASCII-art style syntax consisting of brackets, dashes and arrows.
This pattern finds all nodes with a label of Person
, that have an outgoing ACTED_IN
relationship to a node with a label of :Movie
:
![(p:Person)-[r:ACTED_IN]→(m:Movie)]({cdn-url}/neo4j-fundamentals/modules/2-querying-graphs/lessons/2-pattern-matching/images/pattern-1.png)
If you break this pattern down, it contains nodes, relationships and variables.
Nodes
Nodes in the pattern are expressed with parentheses - ( )

Labels
Inside the parentheses you can define information about the node, for example the label(s) or properties the node should contain.

Labels are prefixed a colon - (:Label)
The pattern contains two nodes (:Person)
and (:Movie)
.
Relationships
Relationships are drawn with two dashes (--
) and an arrow to specify the direction (<
or >
) - - →
.

Relationship types
Relationship information is contained within square brackets -[ ]
.

The relationship type is prefixed with a colon - [:TYPE]
The pattern contains one relationship -[:ACTED_IN]→
between (:Person)
and (:Movie)
nodes.
Recap
To recap, this pattern:
![(p:Person)-[r:ACTED_IN]→(m:Movie)]({cdn-url}/neo4j-fundamentals/modules/2-querying-graphs/lessons/2-pattern-matching/images/pattern-1.png)
Finds (:Person)
nodes, that have an -[:ACTED_IN]→
relationship, to (:Movie)
nodes.
Variables
The nodes and relationships in the pattern are assigned to variables.

These variables are positioned before the information about the node or relationship.
-
p
- the:Person
node -
r
- the:ACTED_IN
relationship -
m
- the:Movie
node
MATCH-ing
The MATCH
clause is used to find patterns in the data.
Review this Cypher statement, you should be able to identify:
-
The pattern being used.
-
A
WHERE
clause which filters the results. -
The variables used in the
RETURN
clause.
MATCH (p:Person)-[r:ACTED_IN]->(m:Movie)
WHERE p.name = 'Tom Hanks'
RETURN p,r,m
MATCH-ing
Patterns can be as simple as a single node, or contain multiple relationships.
This example finds all people who have acted in movies with 'Tom Hanks', and uses the RETURN
clause to define the properties.
MATCH (p:Person)-[:ACTED_IN]->(m:Movie)<-[r:ACTED_IN]-(p2:Person)
WHERE p.name = 'Tom Hanks'
RETURN p2.name AS actor, m.title AS movie, r.role AS role
The pattern uses the ACTED_IN relationship to find the movies Tom Hanks is in, and then a second time to find the actors in the movies with Tom Hanks,
The keyword AS
is used to define an alias, for example, the property p2.name
will be returned as actor
.
Next
Summary
In this lesson, you learned about Cypher patterns and how to read data.
In the next lesson, you will learn how to create data.