Neo4j入门案例:三星堆
创建一个关于三星堆的知识图谱可以是一个非常有趣的项目,它可以帮助理解如何使用Neo4j来存储和查询复杂的关系数据。三星堆文化以其独特的青铜器、金器和其他文物而闻名,这为我们提供了一个丰富的历史背景来构建知识图谱。
数据模型定义
实体类型(节点)
- 遗址 (Site)
- 文物 (Artifact)
- 考古学家 (Archaeologist)
- 文化 (Culture)
- 时期 (Period)
- 材料 (Material)
关系类型
- 发现于 (DiscoveredAt)
- 属于 (BelongsTo)
- 研究者 (ResearchedBy)
- 存在于 (ExistsIn)
- 制成于 (MadeOf)
数据准备
假设我们有如下数据:
- 遗址: “三星堆遗址”
- 文物: “青铜大立人”,“黄金面具”,“玉琮”,“象牙”,“铜神树”
- 考古学家: “李某某”,“张某某”
- 文化: “三星堆文化”
- 时期: “新石器时代晚期至商周时期”
- 材料: “青铜”,“黄金”,“玉石”,“象牙”
创建知识图谱
创建节点
CREATE (s:Site {name: "三星堆遗址"})
CREATE (c:Culture {name: "三星堆文化"})
CREATE (p:Period {name: "新石器时代晚期至商周时期"})
CREATE (a1:Artifact {name: "青铜大立人", description: "三星堆出土的重要文物之一"})
CREATE (a2:Artifact {name: "黄金面具", description: "三星堆出土的精美黄金制品"})
CREATE (a3:Artifact {name: "玉琮", description: "三星堆出土的玉质文物"})
CREATE (a4:Artifact {name: "象牙", description: "三星堆出土的象牙制品"})
CREATE (a5:Artifact {name: "铜神树", description: "三星堆出土的青铜树形器"})
CREATE (m1:Material {name: "青铜"})
CREATE (m2:Material {name: "黄金"})
CREATE (m3:Material {name: "玉石"})
CREATE (m4:Material {name: "象牙"})
CREATE (r1:Archaeologist {name: "李某某", institution: "四川大学考古学院"})
CREATE (r2:Archaeologist {name: "张某某", institution: "北京大学考古文博学院"})
创建关系
MATCH (a1:Artifact), (s:Site)
WHERE a1.name = "青铜大立人" AND s.name = "三星堆遗址"
CREATE (a1)-[:DiscoveredAt]->(s)
MATCH (a1:Artifact), (c:Culture)
WHERE a1.name = "青铜大立人" AND c.name = "三星堆文化"
CREATE (a1)-[:BelongsTo]->(c)
MATCH (a1:Artifact), (p:Period)
WHERE a1.name = "青铜大立人" AND p.name = "新石器时代晚期至商周时期"
CREATE (a1)-[:ExistsIn]->(p)
MATCH (a1:Artifact), (m1:Material)
WHERE a1.name = "青铜大立人" AND m1.name = "青铜"
CREATE (a1)-[:MadeOf]->(m1)
MATCH (a2:Artifact), (s:Site)
WHERE a2.name = "黄金面具" AND s.name = "三星堆遗址"
CREATE (a2)-[:DiscoveredAt]->(s)
MATCH (a2:Artifact), (c:Culture)
WHERE a2.name = "黄金面具" AND c.name = "三星堆文化"
CREATE (a2)-[:BelongsTo]->(c)
MATCH (a2:Artifact), (p:Period)
WHERE a2.name = "黄金面具" AND p.name = "新石器时代晚期至商周时期"
CREATE (a2)-[:ExistsIn]->(p)
MATCH (a2:Artifact), (m2:Material)
WHERE a2.name = "黄金面具" AND m2.name = "黄金"
CREATE (a2)-[:MadeOf]->(m2)
MATCH (a3:Artifact), (s:Site)
WHERE a3.name = "玉琮" AND s.name = "三星堆遗址"
CREATE (a3)-[:DiscoveredAt]->(s)
MATCH (a3:Artifact), (c:Culture)
WHERE a3.name = "玉琮" AND c.name = "三星堆文化"
CREATE (a3)-[:BelongsTo]->(c)
MATCH (a3:Artifact), (p:Period)
WHERE a3.name = "玉琮" AND p.name = "新石器时代晚期至商周时期"
CREATE (a3)-[:ExistsIn]->(p)
MATCH (a3:Artifact), (m3:Material)
WHERE a3.name = "玉琮" AND m3.name = "玉石"
CREATE (a3)-[:MadeOf]->(m3)
MATCH (a4:Artifact), (s:Site)
WHERE a4.name = "象牙" AND s.name = "三星堆遗址"
CREATE (a4)-[:DiscoveredAt]->(s)
MATCH (a4:Artifact), (c:Culture)
WHERE a4.name = "象牙" AND c.name = "三星堆文化"
CREATE (a4)-[:BelongsTo]->(c)
MATCH (a4:Artifact), (p:Period)
WHERE a4.name = "象牙" AND p.name = "新石器时代晚期至商周时期"
CREATE (a4)-[:ExistsIn]->(p)
MATCH (a4:Artifact), (m4:Material)
WHERE a4.name = "象牙" AND m4.name = "象牙"
CREATE (a4)-[:MadeOf]->(m4)
MATCH (a5:Artifact), (s:Site)
WHERE a5.name = "铜神树" AND s.name = "三星堆遗址"
CREATE (a5)-[:DiscoveredAt]->(s)
MATCH (a5:Artifact), (c:Culture)
WHERE a5.name = "铜神树" AND c.name = "三星堆文化"
CREATE (a5)-[:BelongsTo]->(c)
MATCH (a5:Artifact), (p:Period)
WHERE a5.name = "铜神树" AND p.name = "新石器时代晚期至商周时期"
CREATE (a5)-[:ExistsIn]->(p)
MATCH (a5:Artifact), (m1:Material)
WHERE a5.name = "铜神树" AND m1.name = "青铜"
CREATE (a5)-[:MadeOf]->(m1)
MATCH (r1:Archaeologist), (a1:Artifact)
WHERE r1.name = "李某某" AND a1.name = "青铜大立人"
CREATE (a1)-[:ResearchedBy]->(r1)
MATCH (r1:Archaeologist), (a2:Artifact)
WHERE r1.name = "李某某" AND a2.name = "黄金面具"
CREATE (a2)-[:ResearchedBy]->(r1)
MATCH (r2:Archaeologist), (a3:Artifact)
WHERE r2.name = "张某某" AND a3.name = "玉琮"
CREATE (a3)-[:ResearchedBy]->(r2)
MATCH (r2:Archaeologist), (a4:Artifact)
WHERE r2.name = "张某某" AND a4.name = "象牙"
CREATE (a4)-[:ResearchedBy]->(r2)
MATCH (r2:Archaeologist), (a5:Artifact)
WHERE r2.name = "张某某" AND a5.name = "铜神树"
CREATE (a5)-[:ResearchedBy]->(r2)
查询及推理
查询示例
- 查找所有在三星堆遗址发现的文物
1MATCH (a:Artifact)-[r:DiscoveredAt]->(s:Site) WHERE s.name = "三星堆遗址"
2RETURN a.name
- 找出某个考古学家研究的所有文物
-
1MATCH (a:Artifact)-[r:ResearchedBy]->(r1:Archaeologist) WHERE r1.name = "李某某" 2RETURN a.name
- 分析特定时期内出现的文物类型及其材料分布情况
1MATCH (a:Artifact)-[r1:ExistsIn]->(p:Period) WHERE p.name = "新石器时代晚期至商周时期"
2OPTIONAL MATCH (a)-[r2:MadeOf]->(m:Material)
3RETURN a.name, collect(m.name) AS Materials
这些查询可以帮助理解如何从知识图谱中提取有用的信息,并进行进一步的研究。