Reading Graphs

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:

Person and Movie Nodes

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.

Person and Movie nodes connected by ACTED_IN and DIRECTED relationships.

Genres

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.

Movie and Genre nodes connected by IN_GENRE and RATED relationships.

User ratings

The graph also contains User nodes and over 100,000 movies ratings.

User nodes are connected to Movie nodes by a RATED relationship.

User and Movie nodes connected by RATED relationships.

Explore the Graph

GraphAcademy has created a Neo4j database instance for you.

Run the following Cypher statement to find a Person node with the name attribute 'Tom Hanks'.

cypher
Tom Hanks
MATCH (n:Person)
WHERE n.name = 'Tom Hanks'
RETURN n

People

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.

The result of the above query showing a single node labelled Tom Hanks

Expand Relationships

Expand the node’s relationships by doubling click the node.

You will see that there are ACTED_IN and DIRECTED relationships to Movie nodes.

The Tom Hanks node connected to Movie nodes with ACTED_IN relationships

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.

cypher
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.

The Toy Story Movie node and Person nodes connected by ACTED_IN relationships

Genres

This Cypher statement uses the [IN_GENRE] relationship to find related Genre nodes.

cypher
Movie Genres
MATCH (m:Movie)-[r:IN_GENRE]->(g:Genre)
WHERE m.title = 'Toy Story'
RETURN m, r, g
The Toy Story Movie node and Genre nodes connected by the IN_GENRE relationship

Tabular Data

You can return tabular data by including the properties of the nodes.

cypher
List Movie Genres
MATCH (m:Movie)-[r:IN_GENRE]->(g:Genre)
WHERE m.title = 'Toy Story'
RETURN m.title, g.name

Experiment

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.

cypher
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".

A single User node connected to Movie nodes by RATED relationships.

User Ratings

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:

cypher
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"

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.