Skip to content

Social Network Graph

This tutorial builds a social network with users, friendships, posts and likes.

Topics Covered

  • Creating nodes with labels and properties
  • Creating relationships between nodes
  • Querying connected data
  • Aggregating graph data

Prerequisites

Setup

import grafeo

db = grafeo.GrafeoDB()

Step 1: Create Users

db.execute("""
    INSERT (:User {
        id: 1,
        name: 'Alix',
        email: 'alix@example.com',
        joined: '2023-01-15'
    })
    INSERT (:User {
        id: 2,
        name: 'Gus',
        email: 'gus@example.com',
        joined: '2023-02-20'
    })
    INSERT (:User {
        id: 3,
        name: 'Harm',
        email: 'harm@example.com',
        joined: '2023-03-10'
    })
    INSERT (:User {
        id: 4,
        name: 'Dave',
        email: 'dave@example.com',
        joined: '2023-04-05'
    })
""")

Step 2: Create Friendships

# Alix and Gus are friends
db.execute("""
    MATCH (a:User {name: 'Alix'}), (b:User {name: 'Gus'})
    INSERT (a)-[:FRIENDS_WITH {since: '2023-03-01'}]->(b)
    INSERT (b)-[:FRIENDS_WITH {since: '2023-03-01'}]->(a)
""")

# Alix and Harm are friends
db.execute("""
    MATCH (a:User {name: 'Alix'}), (c:User {name: 'Harm'})
    INSERT (a)-[:FRIENDS_WITH {since: '2023-04-15'}]->(c)
    INSERT (c)-[:FRIENDS_WITH {since: '2023-04-15'}]->(a)
""")

# Gus and Dave are friends
db.execute("""
    MATCH (b:User {name: 'Gus'}), (d:User {name: 'Dave'})
    INSERT (b)-[:FRIENDS_WITH {since: '2023-05-20'}]->(d)
    INSERT (d)-[:FRIENDS_WITH {since: '2023-05-20'}]->(b)
""")

Step 3: Create Posts

db.execute("""
    INSERT (:Post {
        id: 1,
        content: 'Hello everyone! Excited to join this network.',
        created: '2023-03-15'
    })
    INSERT (:Post {
        id: 2,
        content: 'Just discovered Grafeo - amazing graph database!',
        created: '2023-04-01'
    })
    INSERT (:Post {
        id: 3,
        content: 'Graph databases make relationships easy to model.',
        created: '2023-04-10'
    })
""")

# Link posts to authors
db.execute("""
    MATCH (u:User {name: 'Alix'}), (p:Post {id: 1})
    INSERT (u)-[:POSTED]->(p)
""")
db.execute("""
    MATCH (u:User {name: 'Gus'}), (p:Post {id: 2})
    INSERT (u)-[:POSTED]->(p)
""")
db.execute("""
    MATCH (u:User {name: 'Harm'}), (p:Post {id: 3})
    INSERT (u)-[:POSTED]->(p)
""")

Step 4: Add Likes

# Gus likes Alix's post
db.execute("""
    MATCH (u:User {name: 'Gus'}), (p:Post {id: 1})
    INSERT (u)-[:LIKES {at: '2023-03-16'}]->(p)
""")

# Harm likes Alix's and Gus's posts
db.execute("""
    MATCH (u:User {name: 'Harm'}), (p:Post {id: 1})
    INSERT (u)-[:LIKES {at: '2023-03-17'}]->(p)
""")
db.execute("""
    MATCH (u:User {name: 'Harm'}), (p:Post {id: 2})
    INSERT (u)-[:LIKES {at: '2023-04-02'}]->(p)
""")

# Dave likes all posts
db.execute("""
    MATCH (u:User {name: 'Dave'}), (p:Post)
    INSERT (u)-[:LIKES]->(p)
""")

Querying the Social Network

Find Alix's Friends

result = db.execute("""
    MATCH (alix:User {name: 'Alix'})-[:FRIENDS_WITH]->(friend)
    RETURN friend.name, friend.email
""")

print("Alix's friends:")
for row in result:
    print(f"  - {row['friend.name']} ({row['friend.email']})")

Find Friend Recommendations (Friends of Friends)

result = db.execute("""
    MATCH (alix:User {name: 'Alix'})-[:FRIENDS_WITH]->()-[:FRIENDS_WITH]->(fof)
    WHERE fof <> alix
      AND NOT (alix)-[:FRIENDS_WITH]->(fof)
    RETURN DISTINCT fof.name AS recommendation
""")

print("Friend recommendations for Alix:")
for row in result:
    print(f"  - {row['recommendation']}")

Find Most Liked Posts

result = db.execute("""
    MATCH (p:Post)<-[:LIKES]-(u:User)
    MATCH (author)-[:POSTED]->(p)
    RETURN p.content AS post, author.name AS author, count(u) AS likes
    ORDER BY likes DESC
""")

print("Posts by popularity:")
for row in result:
    print(f"  {row['likes']} likes: '{row['post'][:50]}...' by {row['author']}")

Find Users Who Liked Friends' Posts

result = db.execute("""
    MATCH (u:User)-[:FRIENDS_WITH]->(friend)-[:POSTED]->(p)<-[:LIKES]-(u)
    RETURN u.name AS user, friend.name AS friend, p.content AS post
""")

print("Users who liked their friends' posts:")
for row in result:
    print(f"  {row['user']} liked {row['friend']}'s post")

Next Steps