Neo4j入门案例:西游记

创建一个基于《西游记》中"孙悟空"的黑神话版本的知识图谱。这个图谱将会包括《西游记》中的一些主要角色、地点、事件以及它们之间的关系。我们将创建至少10个节点和20个关系,并提供相应的Cypher语句。

数据模型定义

实体类型(节点)
  1. 角色 (Character)
  2. 地点 (Location)
  3. 事件 (Event)
  4. 武器 (Weapon)
  5. 技能 (Skill)
关系类型
  1. 居住于 (LivesIn)
  2. 参与 (ParticipatesIn)
  3. 使用 (Uses)
  4. 拥有 (Has)
  5. 敌对 (FoesWith)
  6. 朋友 (FriendsWith)

数据准备

假设我们有如下数据:

  • 角色: "孙悟空","唐僧","猪八戒","沙僧","白骨精","牛魔王"
  • 地点: "花果山","五行山","天宫","火焰山"
  • 事件: "取经之路","大闹天宫","三打白骨精"
  • 武器: "金箍棒","九齿钉耙","降妖杖"
  • 技能: "七十二变","筋斗云","定身法"

创建知识图谱

创建节点
CREATE (s1:Character {name: "孙悟空", role: "主角"})
CREATE (s2:Character {name: "唐僧", role: "师傅"})
CREATE (s3:Character {name: "猪八戒", role: "徒弟"})
CREATE (s4:Character {name: "沙僧", role: "徒弟"})
CREATE (s5:Character {name: "白骨精", role: "妖怪"})
CREATE (s6:Character {name: "牛魔王", role: "妖怪"})
CREATE (l1:Location {name: "花果山", description: "孙悟空的家乡"})
CREATE (l2:Location {name: "五行山", description: "孙悟空被压之地"})
CREATE (l3:Location {name: "天宫", description: "神仙居住的地方"})
CREATE (l4:Location {name: "火焰山", description: "牛魔王的领地"})
CREATE (e1:Event {name: "取经之路", description: "唐僧师徒四人的旅途"})
CREATE (e2:Event {name: "大闹天宫", description: "孙悟空与天庭的冲突"})
CREATE (e3:Event {name: "三打白骨精", description: "孙悟空与白骨精的战斗"})
CREATE (w1:Weapon {name: "金箍棒", description: "孙悟空的法宝"})
CREATE (w2:Weapon {name: "九齿钉耙", description: "猪八戒的武器"})
CREATE (w3:Weapon {name: "降妖杖", description: "沙僧的武器"})
CREATE (sk1:Skill {name: "七十二变", description: "孙悟空的技能"})
CREATE (sk2:Skill {name: "筋斗云", description: "孙悟空的技能"})
CREATE (sk3:Skill {name: "定身法", description: "孙悟空的技能"})
创建关系
MATCH (s1:Character), (l1:Location)
WHERE s1.name = "孙悟空" AND l1.name = "花果山"
CREATE (s1)-[:LivesIn]->(l1)

MATCH (s1:Character), (e1:Event)
WHERE s1.name = "孙悟空" AND e1.name = "取经之路"
CREATE (s1)-[:ParticipatesIn]->(e1)

MATCH (s1:Character), (e2:Event)
WHERE s1.name = "孙悟空" AND e2.name = "大闹天宫"
CREATE (s1)-[:ParticipatesIn]->(e2)

MATCH (s1:Character), (e3:Event)
WHERE s1.name = "孙悟空" AND e3.name = "三打白骨精"
CREATE (s1)-[:ParticipatesIn]->(e3)

MATCH (s1:Character), (w1:Weapon)
WHERE s1.name = "孙悟空" AND w1.name = "金箍棒"
CREATE (s1)-[:Uses]->(w1)

MATCH (s1:Character), (sk1:Skill)
WHERE s1.name = "孙悟空" AND sk1.name = "七十二变"
CREATE (s1)-[:Has]->(sk1)

MATCH (s1:Character), (sk2:Skill)
WHERE s1.name = "孙悟空" AND sk2.name = "筋斗云"
CREATE (s1)-[:Has]->(sk2)

MATCH (s1:Character), (sk3:Skill)
WHERE s1.name = "孙悟空" AND sk3.name = "定身法"
CREATE (s1)-[:Has]->(sk3)

MATCH (s2:Character), (l2:Location)
WHERE s2.name = "唐僧" AND l2.name = "五行山"
CREATE (s2)-[:LivesIn]->(l2)

MATCH (s2:Character), (e1:Event)
WHERE s2.name = "唐僧" AND e1.name = "取经之路"
CREATE (s2)-[:ParticipatesIn]->(e1)

MATCH (s3:Character), (l1:Location)
WHERE s3.name = "猪八戒" AND l1.name = "花果山"
CREATE (s3)-[:LivesIn]->(l1)

MATCH (s3:Character), (e1:Event)
WHERE s3.name = "猪八戒" AND e1.name = "取经之路"
CREATE (s3)-[:ParticipatesIn]->(e1)

MATCH (s3:Character), (w2:Weapon)
WHERE s3.name = "猪八戒" AND w2.name = "九齿钉耙"
CREATE (s3)-[:Uses]->(w2)

MATCH (s4:Character), (l1:Location)
WHERE s4.name = "沙僧" AND l1.name = "花果山"
CREATE (s4)-[:LivesIn]->(l1)

MATCH (s4:Character), (e1:Event)
WHERE s4.name = "沙僧" AND e1.name = "取经之路"
CREATE (s4)-[:ParticipatesIn]->(e1)

MATCH (s4:Character), (w3:Weapon)
WHERE s4.name = "沙僧" AND w3.name = "降妖杖"
CREATE (s4)-[:Uses]->(w3)

MATCH (s5:Character), (l4:Location)
WHERE s5.name = "白骨精" AND l4.name = "火焰山"
CREATE (s5)-[:LivesIn]->(l4)

MATCH (s5:Character), (e3:Event)
WHERE s5.name = "白骨精" AND e3.name = "三打白骨精"
CREATE (s5)-[:ParticipatesIn]->(e3)

MATCH (s6:Character), (l4:Location)
WHERE s6.name = "牛魔王" AND l4.name = "火焰山"
CREATE (s6)-[:LivesIn]->(l4)

MATCH (s1:Character), (s5:Character)
WHERE s1.name = "孙悟空" AND s5.name = "白骨精"
CREATE (s1)-[:FoesWith]->(s5)

MATCH (s1:Character), (s2:Character)
WHERE s1.name = "孙悟空" AND s2.name = "唐僧"
CREATE (s1)-[:FriendsWith]->(s2)

查询及推理

查询示例
  • 查找所有居住在花果山的角色

  MATCH (c:Character)-[r:LivesIn]->(l:Location) WHERE l.name = "花果山"
  RETURN c.name

找出孙悟空拥有的所有技能
*

  MATCH (s1:Character)-[r:Has]->(sk:Skill) WHERE s1.name = "孙悟空"
  RETURN sk.name

分析参与了"取经之路"事件的所有角色及其武器
*

  MATCH (c:Character)-[r:ParticipatesIn]->(e:Event)<-[r2:Uses]-(w:Weapon) WHERE e.name = "取经之路"
  RETURN c.name, w.name
推理示例
  • 寻找所有与孙悟空敌对的角色及其居住地点
  MATCH (s1:Character)-[r:FoesWith]->(s2:Character)-[r2:LivesIn]->(l:Location) WHERE s1.name = "孙悟空"
  RETURN s2.name, l.name

找出参与了"大闹天宫"事件的角色以及他们拥有的技能
*

  MATCH (c:Character)-[r:ParticipatesIn]->(e:Event)<-[r2:Has]-(sk:Skill) WHERE e.name = "大闹天宫"
  RETURN c.name, sk.name
相关推荐
麦麦大数据18 小时前
基于vue+neo4j 的中药方剂知识图谱可视化系统
vue.js·知识图谱·neo4j
engchina1 天前
Neo4j 和 Python 初学者指南:如何使用可选关系匹配优化 Cypher 查询
数据库·python·neo4j
engchina1 天前
使用 Cypher 查询语言在 Neo4j 中查找最短路径
数据库·neo4j
engchina3 天前
Neo4j数据库清理指南:如何安全地删除所有节点和索引
数据库·neo4j
麦麦大数据4 天前
vue+django+neo4j航班智能问答知识图谱可视化系统
django·vue·echarts·neo4j·智能问答·ltp·航班
engchina5 天前
docker 启动 neo4j
docker·容器·neo4j
阿梦Anmory7 天前
[neo4j报错]py2neo.errors.ClientError: [Request.Invalid] Not Found解决方案
neo4j
lisacumt8 天前
【neo4j】 neo4j cypher单一语句 optional 可选操作的技巧
neo4j
qa浪涛11 天前
基于知识图谱的教学案例问答系统
人工智能·django·知识图谱·neo4j
qa浪涛12 天前
基于知识图谱的美食推荐系统
人工智能·python·django·知识图谱·neo4j·美食