py2neo返回创建的节点和关系的数量 [英] py2neo return number of nodes and relationships created
问题描述
我需要创建一个python函数,以便它向图添加节点和关系并返回创建的节点和关系的数量.
I need to create a python function such that it adds nodes and relationship to a graph and returns the number of created nodes and relationships.
我已经使用graph.cypher.execute()添加了节点和关系.
I have added the nodes and relationship using graph.cypher.execute().
arr_len = len(dic_st[story_id]['PER'])
for j in dic_st[story_id]['PER']:
graph.cypher.execute("MERGE (n:PER {name:{name}})",name = j[0].upper()) #creating the nodes of PER in the story
print j[0]
for j in range(0,arr_len):
for k in range(j+1,arr_len):
graph.cypher.execute("MATCH (p1:PER {name:{name1}}), (p2:PER {name:{name2}}) WHERE upper(p1.name)<>upper(p2.name) CREATE UNIQUE (p1)-[r:in_same_doc {st_id:{st_id}}]-(p2)", name1=dic_st[story_id]['PER'][j][0].upper(),name2=dic_st[story_id]['PER'][k][0].upper(),st_id=story_id) #linking the edges for PER nodes
我需要返回创建的新节点和关系的数量.
What I need is to return the number of new nodes and relationships created.
我从neo4j文档中了解到,在cypher中有MERGE的名称为"ON CREATE"和"ON MATCH",但这并不是很有用. neo4j的浏览器界面实际上显示了已更新的节点数和关系.这是我需要返回的内容,但是我访问它的方式还不够.
What I get to know from the neo4j documentation is that there is something called "ON CREATE" and "ON MATCH" for MERGE in cypher, but thats not being very useful. The browser interface for neo4j do actually shows the number of nodes and relationship updated. This is what I need to return, but I am not getting quite the way for it to access it.
请帮忙.
推荐答案
使用x = session.run(...)
执行查询后,您可以使用x.summary.counters
来获得马丁·佩鲁斯(Martin Perusse)答案中注明的统计信息.请参见文档此处.
After executing your query using x = session.run(...)
you can use x.summary.counters
to get the statistics noted in Martin Perusse's answer. See the documentation here.
在旧版本中,计数器在x._summary.counters
下作为专用"字段提供.
In older versions the counters are available as a "private" field under x._summary.counters
.
这篇关于py2neo返回创建的节点和关系的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!