Goals This guide explains the basic concepts of Cypher, Neo4j’s query language, including how to create and query graphs. You should be able to read and understand Cypher queries after finishing this guide. Prerequisites You should have familiarized yourself with… Read more →

Goals
This guide explains the basic concepts of Cypher, Neo4j’s query language, including how to create and query graphs. You should be able to read and understand Cypher queries after finishing this guide.
Prerequisites
You should have familiarized yourself with Graph Databases and the Property Graph Model.
Beginner


Pop-Cultural Connections

The Movie Graph is a mini graph application containing actors and directors that are related through the movies they’ve collaborated on. It is available in your Neo4j Browser if you execute :play movie-graph. It is helpful if you run the queries as you go along, but you should understand them even so.

This guide will show you how to:

  1. Create: insert movie data into the graph
  2. Find: retrieve individual movies and actors
  3. Query: discover related actors and directors
  4. Solve: the Bacon Path

Create

Hidden below this text is a giant code block containing a single Cypher query statement composed of multiple CREATE clauses. This will create the movie graph.

(You can unfold it with the little [role=’icon-plus-sign-alt’]plus button)

Find

Example queries for finding individual nodes.

  1. Look at every query example
  2. Run the query with the play button
  3. Notice the syntax pattern
  4. Try looking for other movies or actors

Find the Person Named “Tom Hanks”…​

MATCH (tom:Person)
WHERE tom.name = "Tom Hanks"
RETURN tom

Find the Movie Titled “Cloud Atlas”…​

MATCH (cloudAtlas:Movie {title: "Cloud Atlas"})
RETURN cloudAtlas

Find 10 People…​

MATCH (people:Person)
RETURN people.name LIMIT 10

Find Movies Released in the 1990s…​

MATCH (nineties:Movie)
WHERE nineties.released > 1990 AND nineties.released < 2000
RETURN nineties.title

Query

Finding patterns within the graph.

  1. Actors are people who acted in movies
  2. Directors are people who directed a movie
  3. What other relationships exist?

List All Tom Hanks Movies…​

MATCH (tom:Person {name: "Tom Hanks"})-[:ACTED_IN]->(tomHanksMovies)
RETURN tom,tomHanksMovies

Who Directed “Cloud Atlas”?

MATCH (cloudAtlas:Movie {title: "Cloud Atlas"})<-[:DIRECTED]-(directors)
RETURN directors.name

Tom Hanks’ Co-Actors…​

MATCH (tom:Person {name:"Tom Hanks"})-[:ACTED_IN]->(m)<-[:ACTED_IN]-(coActors)
RETURN tom, m, coActors

Solve the Six Degrees Question

You’ve heard of the classic “Six Degrees of Kevin Bacon”? That is simply a shortest path query called the “Bacon Path”.

  1. Variable length patterns
  2. Built-in shortestPath() algorithm

Movies and Actors up to Three Hops Away from Kevin Bacon

MATCH (bacon:Person {name:"Kevin Bacon"})-[*1..3]-(hollywood)
RETURN DISTINCT hollywood

The Bacon Path to Meg Ryan

MATCH p=shortestPath(
  (bacon:Person {name:"Kevin Bacon"})-[*]-(meg:Person {name:"Meg Ryan"})
)
RETURN p

Clean Up

When you’re done experimenting, you can remove the movie data set.

  1. Nodes can’t be deleted if relationships to them exist
  2. Delete both nodes and relationships together
This will remove all Person and Movie nodes!

Delete All Movie and Person Nodes and their Relationships

MATCH (a:Person),(m:Movie)
OPTIONAL MATCH (a)-[r1]-(), (m)-[r2]-()
DELETE a,r1,m,r2

Prove that the Movie Graph is Gone

MATCH (n)
RETURN count(*)