|
|
|
|
from py2neo import Graph, Node, Relationship, Subgraph
|
|
|
|
|
|
|
|
|
|
# 连接数据库
|
|
|
|
|
db = Graph("neo4j://127.0.0.1:7687", auth=("neo4j", "DsideaL4r5t6y7u"))
|
|
|
|
|
|
|
|
|
|
# 修正版本查询代码
|
|
|
|
|
version = db.run("CALL dbms.components() YIELD versions UNWIND versions AS version RETURN version").evaluate()
|
|
|
|
|
print(f"Neo4j 版本: {version}")
|
|
|
|
|
|
|
|
|
|
# 清空数据
|
|
|
|
|
db.run("MATCH (n) DETACH DELETE n")
|
|
|
|
|
|
|
|
|
|
# 分步删除约束和索引
|
|
|
|
|
try:
|
|
|
|
|
# 删除约束
|
|
|
|
|
constraints = db.run("SHOW CONSTRAINTS YIELD name").data()
|
|
|
|
|
for constr in constraints:
|
|
|
|
|
db.run(f"DROP CONSTRAINT `{constr['name']}`")
|
|
|
|
|
|
|
|
|
|
# 删除索引
|
|
|
|
|
indexes = db.run("SHOW INDEXES YIELD name, type WHERE type <> 'LOOKUP'").data()
|
|
|
|
|
for idx in indexes:
|
|
|
|
|
db.run(f"DROP INDEX `{idx['name']}`")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"删除操作失败: {e}")
|
|
|
|
|
|
|
|
|
|
# 创建节点
|
|
|
|
|
node_1 = Node("英雄", name="张无忌")
|
|
|
|
|
node_2 = Node("英雄", name="杨逍", power=100)
|
|
|
|
|
node_3 = Node("派别", name="明教")
|
|
|
|
|
|
|
|
|
|
# 创建关系
|
|
|
|
|
relations = [
|
|
|
|
|
(node_1, '教主', node_2),
|
|
|
|
|
(node_3, '统领', node_1), # 修正原代码可能的逻辑关系
|
|
|
|
|
(node_2, '师出', node_3)
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
# 构建子图并提交
|
|
|
|
|
subgraph = Subgraph(
|
|
|
|
|
nodes=[node_1, node_2, node_3],
|
|
|
|
|
relationships=[Relationship(r[0], r[1], r[2]) for r in relations]
|
|
|
|
|
)
|
|
|
|
|
db.create(subgraph)
|
|
|
|
|
|
|
|
|
|
from py2neo import Path
|
|
|
|
|
# 建一个路径:比如按照该路径查询,或者遍历的结果保存为路径
|
|
|
|
|
node_4, node_5, node_6 = Node(name='阿大'), Node(name='阿二'), Node(name='阿三')
|
|
|
|
|
path_1 = Path(node_4, '小弟', node_5, Relationship(node_6, "小弟", node_5), node_6)
|
|
|
|
|
db.create(path_1)
|
|
|
|
|
|
|
|
|
|
print(path_1)
|
|
|
|
|
|
|
|
|
|
# 创建一个子图,并通过子图的方式更新数据库
|
|
|
|
|
node_7 = Node('英雄', name='张翠山')
|
|
|
|
|
node_8 = Node('英雄', name='殷素素')
|
|
|
|
|
node_9 = Node('英雄', name='狮王')
|
|
|
|
|
|
|
|
|
|
relationship7 = Relationship(node_1, '生父', node_7)
|
|
|
|
|
relationship8 = Relationship(node_1, '生母', node_8)
|
|
|
|
|
relationship9 = Relationship(node_1, '义父', node_9)
|
|
|
|
|
subgraph_1 = Subgraph(nodes=[node_7, node_8, node_9], relationships=[relationship7, relationship8, relationship9])
|
|
|
|
|
db.create(subgraph_1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 创建一个新的事务
|
|
|
|
|
transaction_1 = db.begin()#事务开始
|
|
|
|
|
|
|
|
|
|
# 创建一个新node
|
|
|
|
|
node_10 = Node('武当',name = '张三丰')
|
|
|
|
|
transaction_1.create(node_10)
|
|
|
|
|
# 创建两个关系:张无忌→(师公)→张三丰 张翠山→(妻子)→殷素素
|
|
|
|
|
relationship_10 = Relationship(node_1,'师公',node_10)
|
|
|
|
|
relationship_11 = Relationship(node_7,'妻子',node_8)
|
|
|
|
|
|
|
|
|
|
transaction_1.create(relationship_10)
|
|
|
|
|
transaction_1.create(relationship_11)
|
|
|
|
|
|
|
|
|
|
transaction_1.commit()#事务提交
|
|
|
|
|
|
|
|
|
|
from py2neo import RelationshipMatcher
|
|
|
|
|
|
|
|
|
|
# 查询某条关系
|
|
|
|
|
relation = RelationshipMatcher(db)
|
|
|
|
|
|
|
|
|
|
# None表示any node哦!不是表示空
|
|
|
|
|
print('=' * 10, 'hate关系查询结果', '=' * 10)
|
|
|
|
|
x = relation.match(nodes=None, r_type='hate')
|
|
|
|
|
for x_ in x:
|
|
|
|
|
print(x_)
|
|
|
|
|
|
|
|
|
|
# 为了便于查询更多类容,使用事务的方法新增一些关系和节点
|
|
|
|
|
transaction_2 = db.begin()
|
|
|
|
|
|
|
|
|
|
node_100 = Node('巾帼', name='赵敏')
|
|
|
|
|
re_100 = Relationship(node_1, 'Love', node_100)
|
|
|
|
|
|
|
|
|
|
node_101 = Node('巾帼', name='周芷若')
|
|
|
|
|
re_101 = Relationship(node_1, 'knows', node_101)
|
|
|
|
|
re_101_ = Relationship(node_101, 'hate', node_100)
|
|
|
|
|
|
|
|
|
|
node_102 = Node('巾帼', name='小昭')
|
|
|
|
|
re_102 = Relationship(node_1, 'konws', node_102)
|
|
|
|
|
|
|
|
|
|
node_103 = Node('巾帼', name='蛛儿')
|
|
|
|
|
re_103 = Relationship(node_103, 'Love', node_1)
|
|
|
|
|
|
|
|
|
|
transaction_2.create(node_100)
|
|
|
|
|
transaction_2.create(re_100)
|
|
|
|
|
transaction_2.create(node_101)
|
|
|
|
|
transaction_2.create(re_101)
|
|
|
|
|
transaction_2.create(re_101_)
|
|
|
|
|
transaction_2.create(node_102)
|
|
|
|
|
transaction_2.create(re_102)
|
|
|
|
|
transaction_2.create(node_103)
|
|
|
|
|
transaction_2.create(re_103)
|
|
|
|
|
|
|
|
|
|
transaction_2.commit()
|
|
|
|
|
# 增加俩关系
|
|
|
|
|
re1_1 = Relationship(node_101, '情敌', node_102)
|
|
|
|
|
re1_2 = Relationship(node_102, '情敌', node_103)
|
|
|
|
|
db.create(re1_1)
|
|
|
|
|
db.create(re1_2)
|
|
|
|
|
|
|
|
|
|
# 情敌查询结果
|
|
|
|
|
print('=' * 10, 'hate关系查询结果', '=' * 10)
|
|
|
|
|
x = relation.match(nodes=None, r_type='情敌')
|
|
|
|
|
for x_ in x:
|
|
|
|
|
print(x_)
|
|
|
|
|
|