Preview

Sarah Mei recently wrote a blog post describing how she and her colleagues modeled a collection of TV shows and a social network. A few of us at Neo felt strongly that these domains were very graphy in nature.

We wanted to see if our intuition was right (we love the Diaspora project and extend our help to you) and so with the help of Cypher, created a mini version of each of the domains that Sarah described.

This is the second of two GraphGists that are part of the response to Sarah’s blog post. If you would like to quickly jump to the first one that details the TV Show Graph data model, you can find it here: TV Show Graph Diaspora example

Social Network Data Model

Social Network Setup

CREATE (rachel:User {name: "Rachel Green"})
CREATE (monica:User {name: "Monica Geller"})
CREATE (phoebe:User {name: "Phoebe Buffay"})
CREATE (joey:User {name: "Joey Tribbiani"})
CREATE (chandler:User {name: "Chandler Bing"})
CREATE (ross:User {name: "Ross Geller"})

CREATE (rachel)-[:FRIEND]->(monica)
CREATE (rachel)-[:FRIEND]->(phoebe)
CREATE (rachel)-[:FRIEND]->(joey)
CREATE (rachel)-[:FRIEND]->(chandler)
CREATE (rachel)-[:FRIEND]->(ross)

CREATE (monica)-[:FRIEND]->(phoebe)
CREATE (monica)-[:FRIEND]->(joey)
CREATE (monica)-[:FRIEND]->(chandler)
CREATE (monica)-[:FRIEND]->(ross)

CREATE (phoebe)-[:FRIEND]->(joey)
CREATE (phoebe)-[:FRIEND]->(chandler)
CREATE (phoebe)-[:FRIEND]->(ross)

CREATE (joey)-[:FRIEND]->(chandler)
CREATE (joey)-[:FRIEND]->(ross)

CREATE (chandler)-[:FRIEND]->(ross)

CREATE (monica)-[:POSTED]->(post0:Post { id: "0", text: "You don't like the game, because you suck at it." })
CREATE (phoebe)-[:POSTED]->(post1:Post { id: "1", text: "Chandler still thinks I'm pregnant and he hasn't asked me how I'm feeling or offered to carry my bags. I feel bad for the woman who ends up with him." })
CREATE (joey)-[:POSTED]->(post2:Post { id: "2", text: "Just because she went to Yale drama, she thinks she's like the greatest actress since, since, sliced bread!" })
CREATE (chandler)-[:POSTED]->(post3:Post { id: "3", text: " In my defense, it was dark and he was a very pretty guy." })
CREATE (rachel)-[:POSTED]->(post4:Post { id: "4", text: "I don't want my baby's first words to be 'How You Doing'" })
CREATE (ross)-[:POSTED]->(post5:Post { id: "5", text: "Chandler entered a Vanilla Ice look-alike contest and *won*!" })

// Comments
CREATE (post0)<-[:COMMENTED{ id: "6", text: "lol" }]-(ross)
CREATE (post1)<-[:COMMENTED{ id: "7", text: "lol" }]-(rachel)
CREATE (post2)<-[:COMMENTED{ id: "8", text: "lol" }]-(chandler)
CREATE (post3)<-[:COMMENTED{ id: "9", text: "lol" }]-(joey)
CREATE (post4)<-[:COMMENTED{ id: "10", text: "lol" }]-(phoebe)
CREATE (post5)<-[:COMMENTED{ id: "11", text: "lol" }]-(monica)


// Likes
CREATE (post0)<-[:LIKED{ id: "12", text: "Ross likes this." }]-(ross)
CREATE (post1)<-[:LIKED{ id: "13", text: "Rachel likes this." }]-(rachel)
CREATE (post2)<-[:LIKED{ id: "14", text: "Chandler likes this." }]-(chandler)
CREATE (post3)<-[:LIKED{ id: "15", text: "Joey likes this." }]-(joey)
CREATE (post4)<-[:LIKED{ id: "16", text: "Phoebe likes this." }]-(phoebe)
CREATE (post5)<-[:LIKED{ id: "17", text: "Monica likes this." }]-(monica)

Now let’s write some queries!

We’ll start with one to find the posts made by Rachel’s friends:

MATCH (u:User)-[:FRIEND]-(f)-[:POSTED]->(post)
WHERE u.name = "Rachel Green"
RETURN f.name AS friend, post.text as content
Loading table...

Our next query might be a slight variation on this to collect the people who have liked a post:

MATCH (u:User)-[:FRIEND]-(f)-[:POSTED]->(post)<-[like:LIKED]-(liker)
WHERE u.name = "Rachel Green"
RETURN f.name AS friend, post.text as content, COLLECT(liker.name) as liked_by
Loading table...

A third query could collect all actions on a post, be they likes or comments:

MATCH (u:User)-[:FRIEND]-(f)-[:POSTED]->(post)<-[a:LIKED|:COMMENTED]->(person)
WHERE u.name = "Rachel Green"
RETURN f.name AS friend, post.text AS content,
       COLLECT({ text: a.text, person: person.name, action: TYPE(a)}) AS actions
Loading table...
Loading graph...