Cypher query in py2neo

余生长醉 提交于 2019-12-07 16:18:21

问题


How do I perform the functions of shortestPath() and allShortestPaths() in py2neo?

In Cypher, I'd execute something like:

START beginning=node(4), end=node(452)
MATCH p = shortestPath(beginning-[*..500]-end)
RETURN p

I've tried what I thought was the equivalent (below), but this doesn't work (these relationships work in cypher, and the node_* objects are indeed the correct nodes

>>> rels = list(graph_db.match(start_node=node_4, end_node=node_452))
>>> rels
[]

回答1:


I don't want to steal jjaderberg's comment but here is how you could run your Cypher query with py2neo. As far as I know graph algorithms are not implemented.

query_string = "START beginning=node(4), end=node(452) 
                MATCH p = shortestPath(beginning-[*..500]-end) 
                RETURN p"

result = neo4j.CypherQuery(graph_db, query_string).execute()

for r in result:
    print type(r) # r is a py2neo.util.Record object
    print type(r.p) # p is a py2neo.neo4j.Path object

You can then get what you need from your path as described here: http://book.py2neo.org/en/latest/paths/



来源:https://stackoverflow.com/questions/19989994/cypher-query-in-py2neo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!