In the previous lesson, you learned how Cypher can be used to read data.
In this lesson, you will explore how you match data with patterns.
Patterns
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
:
If you break this pattern down, it contains nodes, relationships and variables.
Nodes
Nodes in the pattern are expressed with parentheses - ( )
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 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.
To recap, this pattern:
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
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
.
Try it yourself
Emil Eifrem, Neo4j’s CEO, has added himself to the graph as an actor in his favorite movie. Can you find out which movie is his favorite?
Which movie is Emil’s favorite?
This Cypher statement returns the roles and movies 'Tom Hanks' has acted in.
You will need to modify it to use the name
'Emil Eifrem'.
MATCH (p:Person)-[r:ACTED_IN]->(m:Movie)
WHERE p.name = 'Tom Hanks'
RETURN p.name AS person, m.title AS title, r.role AS role
What movie is returned?
-
❏ Back to the Future
-
❏ Blade Runner
-
❏ Gravity
-
✓ The Matrix
Hint
You will need to modify the WHERE
clause from filtering for 'Tom Hanks' to filter for Emil Eifrem
Strings are case-sensitive so you will need to use the correct capitalization of *E*mil *E*ifrem.
The answer is the title
property of the (:Movie)
node.
Solution
You can use the following query to find the title of the Movie that Emil is listed as acted in.
MATCH (p:Person)-[r:ACTED_IN]->(m:Movie)
WHERE p.name = 'Emil Eifrem'
RETURN p.name AS person, m.title AS title, r.role AS role
Click the Run in Sandbox button to reveal the answer in the Sandbox window.
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.