GraphGists

Cypher Introduction - Social Movie Database

Domain model and setup

Our example graph consists of movies with an id, year and title and actors with a name. Actors have an ACTS_IN relationship to movies, which represents the role they played, the role relationship has also a role attribute.

cineasts

We encourage you to enter the Cypher statements in the interactive console manually, but you can also click on the queries to run them.

Like this one

MATCH (n)
RETURN "Hello Graph with "+count(*)+" Nodes!"
as welcome;

First Steps with Cypher

return a single node, by id (The Matrix)

MATCH (movie:Movie {title:"The Matrix"})
RETURN movie;

return the title and id of the matrix node

MATCH (movie:Movie {title:"The Matrix"})
RETURN movie.id, movie.title;

first use of the MATCH clause, show all actors

MATCH (m:Movie {title:"The Matrix"})<-[:ACTS_IN]-(actor)
RETURN actor;

return just the name, order them by name

MATCH (m:Movie {title:"The Matrix"})<-[:ACTS_IN]-(actor)
RETURN actor.name order by actor.name;

first aggregation, count the actors

MATCH (m:Movie {title:"The Matrix"})<-[:ACTS_IN]-(actor)
RETURN count(*);

first filtering only the actors whose names end with "s"

MATCH (m:Movie {title:"The Matrix"})<-[:ACTS_IN]-(actor)
WHERE actor.name =~ ".*s$"
RETURN actor.name;

Interlude: Explore a dataset

some exploratory queries for unknown datasets, don’t do that on live production instances

Count nodes

MATCH (n)
RETURN count(*);

Count relationship types

MATCH (n)-[r]->()
RETURN type(r), count(*);

List all nodes and their relationships

MATCH (n)-[r]->(m)
RETURN n as from, r as `->`, m as to;

Next up is Updating the Graph