Skip to content

Instantly share code, notes, and snippets.

@nawroth
Last active December 21, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nawroth/6318189 to your computer and use it in GitHub Desktop.
Save nawroth/6318189 to your computer and use it in GitHub Desktop.

Social Movie Database

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

So far, we queried the movie data; now let’s update the graph

CREATE (matrix1:Movie {title : 'The Matrix', year : '1999-03-31'})
CREATE (matrix2:Movie {title : 'The Matrix Reloaded', year : '2003-05-07'})
CREATE (matrix3:Movie {title : 'The Matrix Revolutions', year : '2003-10-27'})
CREATE (keanu:Actor {name:'Keanu Reeves'})
CREATE (laurence:Actor {name:'Laurence Fishburne'})
CREATE (carrieanne:Actor {name:'Carrie-Anne Moss'})
CREATE (keanu)-[:ACTS_IN {role : 'Neo'}]->(matrix1)
CREATE (keanu)-[:ACTS_IN {role : 'Neo'}]->(matrix2)
CREATE (keanu)-[:ACTS_IN {role : 'Neo'}]->(matrix3)
CREATE (laurence)-[:ACTS_IN {role : 'Morpheus'}]->(matrix1)
CREATE (laurence)-[:ACTS_IN {role : 'Morpheus'}]->(matrix2)
CREATE (laurence)-[:ACTS_IN {role : 'Morpheus'}]->(matrix3)
CREATE (carrieanne)-[:ACTS_IN {role : 'Trinity'}]->(matrix1)
CREATE (carrieanne)-[:ACTS_IN {role : 'Trinity'}]->(matrix2)
CREATE (carrieanne)-[:ACTS_IN {role : 'Trinity'}]->(matrix3)

We will add ourselves, friends and movie ratings.

Here’s how to add a node for yourself and return it, let’s say your name is ``Me'':

CREATE (me:User {name: "Me"})
RETURN me;

Let’s check if the node is there:

MATCH me:User
WHERE me.name = "Me"
RETURN me.name;

Add a movie rating:

MATCH me:User, movie:Movie
WHERE me.name = "Me" AND movie.title = "The Matrix"
CREATE (me)-[:RATED {stars : 5, comment : "I love that movie!"}]->(movie);

Which movies did I rate?

MATCH me:User, (me)-[rating:RATED]->(movie)
WHERE me.name = "Me"
RETURN movie.title, rating.stars, rating.comment;

We need a friend!

CREATE (friend:User {name: "A Friend"})
RETURN friend;

Add our friendship idempotently, so we can re-run the query without adding it several times. We return the relationship to check that it has not been created several times.

MATCH me:User, friend:User
WHERE me.name = "Me" AND friend.name = "A Friend"
CREATE UNIQUE (me)-[friendship:FRIEND]->(friend)
RETURN friendship;

You can rerun the query, see that it doesn’t change anything the second time!

Let’s update our friendship with a since property:

MATCH me:User, friend:User, (me)-[friendship:FRIEND]->(friend)
WHERE me.name = "Me" AND friend.name = "A Friend"
SET friendship.since='forever'
RETURN friendship;

Let’s pretend us being our friend and wanting to see which movies our friends have rated.

MATCH me:User, (me)-[:FRIEND]-(friend)-[rating:RATED]->(movie)
WHERE me.name = "A Friend"
RETURN movie.title, avg(rating.stars) as stars, collect(rating.comment) as comments, count(*);

That’s too little data, let’s add some more friends and friendships.

MATCH me:User
WHERE me.name = "Me"
FOREACH (i in range(1,10) :
  CREATE (friend:User {name: "Friend " + i}), (me)-[:FRIEND]->(friend));

Show all our friends:

MATCH me:User, (me)-[r:FRIEND]->(friend)
WHERE me.name = "Me"
RETURN type(r) as friendship, friend.name;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment