我试图返回neo4j的完整路径,但是它只能返回python中的起始节点和结束节点,而python会返回neo4j的完整路径。
我的python代码是:
from neo4j import GraphDatabase,basic_auth
uri = "bolt://localhost:7687"
driver = GraphDatabase.driver(uri, auth=("", ""))
session = driver.session()
paths = session.run('''PROFILE
with ['aaa''bbb''ccc''ddd''eee'] as value_list
match (n:Node) where n.value in value_list
with collect(n) as result
unwind result as source
unwind result as target
match paths = shortestpath((source)-[*0..2]-(target)) where source<>target
with paths limit 200
return paths''')
for record in paths:
print(",".join("%s:%s"%(key,record[key]) for key in record.keys()))
session.close()
neo4j中的路径返回为:
"paths"
[{"value":"aaa"},{"value":"ab_relation"},{"value":"bbb"},"value":"bbb"},{"value":"bc_relation"},{"value":"ccc"}]
[{"value":"aaa"},{"value":"ad_relation"},{"value":"ddd"},"value":"ddd"},{"value":"de_relation"},{"value":"eee"}]
但返回python:
paths:<Path start=<Node id=9650694 labels={'Node'} properties={'value': 'aaa'}> end=<Node id=23038409 labels={'Node'} properties={'value': 'ccc'}> size=2>
paths:<Path start=<Node id=9650694 labels={'Node'} properties={'value': 'aaa'}> end=<Node id=9011159 labels={'Node'} properties={'value': 'eee'}> size=2>
我如何拥有完整的路径,而不仅仅是起始节点和结束节点?
答案 0 :(得分:0)
这可以返回完整路径!!!
for record in paths:
#print(record)
relationships = record["path"].relationships
nodes = record["path"].nodes
path = ""
for i in (range(len(relationships))):
path += "{0}-[{1}]->".format(nodes[i]["value"], relationships[i]["value"])
path += nodes[-1]["value"]
wt.write(path+'\n')