Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypher query in py2neo

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
[]
like image 374
jvc26 Avatar asked Dec 15 '22 04:12

jvc26


1 Answers

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/

like image 50
Martin Preusse Avatar answered Dec 21 '22 01:12

Martin Preusse