Skip to content

Neo4j Graph Modeling ​

Nodes vs Relationships ​

Rule: If it has identity and connects to many things, it's a node. If it names how two nodes relate, it's a relationship; a relationship may carry properties (date, price) and connects a source node to a target node; promote it to a node (e.g. Order) when other things must attach to it.

Example: E-commerce

Entities (Nodes):

  • User
  • Product
  • Category
  • Order

Connections (Relationships):

  • User -[:PURCHASED]-> Product
  • User -[:VIEWED]-> Product
  • Product -[:IN_CATEGORY]-> Category
  • Order -[:CONTAINS]-> Product

Pattern 1: Basic Graph Model ​

cypher
// cypher-shell (Neo4j 5); kết thúc mỗi câu bằng dấu ;
CREATE CONSTRAINT user_id IF NOT EXISTS FOR (u:User) REQUIRE u.id IS UNIQUE;
CREATE CONSTRAINT product_id IF NOT EXISTS FOR (p:Product) REQUIRE p.id IS UNIQUE;
CREATE CONSTRAINT category_id IF NOT EXISTS FOR (c:Category) REQUIRE c.id IS UNIQUE;

// Create nodes
CREATE (u:User {id: 123, name: "John", email: "john@example.com"})
CREATE (p:Product {id: 456, name: "Laptop", price: 999})
CREATE (c:Category {id: 1, name: "Electronics"});

// Create relationships
MATCH (u:User {id: 123}), (p:Product {id: 456})
CREATE (u)-[:PURCHASED {date: "2025-01-01", price: 999}]->(p);

MATCH (p:Product {id: 456}), (c:Category {id: 1})
CREATE (p)-[:IN_CATEGORY]->(c);

Pattern 2: Social Network (Followers) ​

cypher
// cypher-shell (Neo4j 5); chạy sau khi tạo constraint User ở Pattern 1
// Create users
CREATE (u1:User {id: 1, name: "Alice"})
CREATE (u2:User {id: 2, name: "Bob"})
CREATE (u3:User {id: 3, name: "Charlie"});

// Relationships
MATCH (u1:User {id: 1}), (u2:User {id: 2}), (u3:User {id: 3})
CREATE (u1)-[:FOLLOWS {since: "2025-01-01"}]->(u2)
CREATE (u2)-[:FOLLOWS {since: "2025-01-02"}]->(u3)
CREATE (u1)-[:FOLLOWS {since: "2025-01-03"}]->(u3);

// Query: Who does Alice follow?
MATCH (alice:User {name: "Alice"})-[:FOLLOWS]->(friend)
RETURN friend.name;

// Query: Who follows Bob?
MATCH (follower:User)-[:FOLLOWS]->(bob:User {name: "Bob"})
RETURN follower.name;

// Query: Friends of friends (2 hops)
MATCH (alice:User {name: "Alice"})-[:FOLLOWS]->()-[:FOLLOWS]->(fof)
WHERE fof <> alice  // Exclude Alice
RETURN DISTINCT fof.name;

Pattern 3: Recommendations (Collaborative Filtering) ​

cypher
// cypher-shell (Neo4j 5); chạy sau khối tạo dữ liệu Pattern 1; thêm dữ liệu tối thiểu
CREATE (:Product {id: 457, name: "Mouse", price: 25});
CREATE (:User {id: 124, name: "Jane"});
MATCH (jane:User {id: 124}), (p:Product) WHERE p.id IN [456, 457]
CREATE (jane)-[:PURCHASED]->(p);

// Recommend products based on similar users' purchases
MATCH (me:User {id: 123})-[:PURCHASED]->(product)<-[:PURCHASED]-(other:User)
MATCH (other)-[:PURCHASED]->(recommendation)
WHERE NOT (me)-[:PURCHASED]->(recommendation)
RETURN recommendation.name, COUNT(*) AS score
ORDER BY score DESC
LIMIT 10;

Phải nhớ:

  • Node = thực thể có định danh; relationship = quan hệ có type và hướng, có thể mang property; cần gắn thêm thứ khác vào quan hệ thì tách thành node.
  • Đặt constraint unique cho khoá tra cứu (id) trước khi MATCH theo nó.
  • Node có số relationship rất lớn (supernode) có thể làm duyệt chậm (chưa đo ở đây); tìm bằng MATCH (n:User) RETURN n.id, COUNT { (n)--() } AS degree ORDER BY degree DESC LIMIT 5 và đo trước khi chốt mô hình.