Using Cypher
Using Cypher
You can use the query language Cypher to interact with a Neo4j graph database.
You are you going to use Cypher to explore a graph database containing:
-
Movies, actors and directors from The Movie Database (TMDB).
-
Movie ratings by users of the website.
The graph contains Person
and Movie
nodes.
Each of Person
node may have one or more outgoing ACTED_IN
and DIRECTED
relationships to a Movie
node.
Movies are categorized into one or more genres, for example, Adventure, Animation, and, Comedy.
Movie
nodes are connected to Genre
nodes by an IN_GENRE
relationship.
The graph also contains User
nodes and over 100,000 movies ratings.
User
nodes are connected to Movie
nodes by a RATED
relationship.
Explore the Graph
GraphAcademy has created a Neo4j database instance for you.
People
Run the following Cypher statement to find a Person
node with the name
attribute 'Tom Hanks'.
MATCH (n:Person)
WHERE n.name = 'Tom Hanks'
RETURN n
Each Person
node has an unique name
property, as well as other properties such as bio
and born
.
View the node properties by selecting the node.
Expand the node’s relationships by doubling click the node.
You will see that there are ACTED_IN
and DIRECTED
relationships to Movie
nodes.
Movies
Each Movie
node has a unique title
property, as well as other properties including plot
, released
and url
.
Run the following Cypher statement to find the movie 'Toy Story' and the people who acted in the movie.
MATCH (m:Movie)<-[r:ACTED_IN]-(p:Person)
WHERE m.title = 'Toy Story'
RETURN m, r, p
The query uses the [ACTED_IN]
relationship to find Person
nodes who have a connection to the Movie
node.
This Cypher statement uses the [IN_GENRE]
relationship to find related Genre
nodes.
MATCH (m:Movie)-[r:IN_GENRE]->(g:Genre)
WHERE m.title = 'Toy Story'
RETURN m, r, g
You can return tabular data by including the properties of the nodes.
MATCH (m:Movie)-[r:IN_GENRE]->(g:Genre)
WHERE m.title = 'Toy Story'
RETURN m.title, g.name
Experiment with these queries. Try and find different movies and people, for example, the Movie
node "Babe" or the Person
node "Emma Stone".
User Ratings
Review this Cypher statement before running it and predict what graph will be returned.
MATCH (u:User)-[r:RATED]->(m:Movie)
WHERE u.name = "Mr. Jason Love"
RETURN u, r, m
Click here to reveal the result
The statement returns all the movies that have been rated by the user "Mr. Jason Love".
The rating the user has given for the movie is stored as a property on the RATED
relationship.
Run this Cypher statement to return a table of movie ratings:
MATCH (u:User)-[r:RATED]->(m:Movie)
WHERE u.name = "Mr. Jason Love"
RETURN u.name, r.rating, m.title
The properties are defined in the RETURN
, for example u.name
will return the name
property of the User
node.
u.name | r.rating | m.title |
---|---|---|
"Mr. Jason Love" |
3.0 |
"Houseguest" |
"Mr. Jason Love" |
3.0 |
"Ed Wood" |
"Mr. Jason Love" |
4.0 |
"Little Women" |
"Mr. Jason Love" |
4.0 |
"Interview with the Vampire: The Vampire Chronicles" |
"Mr. Jason Love" |
5.0 |
"Circle of Friends" |
Check Your Understanding
Node Labels
Which of the following are node labels that are used in the Movie recommendations graph?
-
❏ ACTED_IN
-
❏ DIRECTED
-
✓ Movie
-
✓ Person
Hint
The graph has two labels to represent people and movies.
Hint
The graph has Person
and Movie
node labels.
ACTED_IN
and DIRECTED
are types of relationship used to connect people to movies.
Summary
In this lesson, you learned explored the nodes and relationships in the movie recommendations dataset.
In the next lesson, you will learn how to find patterns in the data using Cypher.