Cypher allows returning paths along with discrete nodes and connections .
The following code demonstart returning a path :
1: from py2neo import neo4j2: from py2neo import node, rel3: from py2neo import *4:5: graph_db = neo4j.GraphDatabaseService("http://localhost:7474/db/data/")6:7: def CreateTheDB ():8: die_hard = graph_db.create(9: node(name="Bruce Willis"),10: node(name="John McClane"),11: node(name="Alan Rickman"),12: node(name="Hans Gruber"),13: node(name="Nakatomi Plaza"),14: rel(0, "PLAYS", 1),15: rel(2, "PLAYS", 3),16: rel(1, "VISITS", 4),17: rel(3, "STEALS_FROM", 4),18: rel(1, "KILLS", 3),19: )20:21: CreateTheDB()22:23: def handle_row(row):24: print ("cypher query result:")25: node = row[0]26: print (node)27: print (type(node) )28:29: theCyperCode = "START n=node(2) MATCH path =a--n RETURN path"30:31: cypher.execute(graph_db, theCyperCode, row_handler=handle_row)
And the result:
cypher query result:
(1)-[:"PLAYS"]->(2)
<class 'py2neo.neo4j.Path'>
cypher query result:
(5)-[:"VISITS"]->(2)
<class 'py2neo.neo4j.Path'>
cypher query result:
(4)-[:"KILLS"]->(2)
<class 'py2neo.neo4j.Path'>
Note the type of the return value :'py2neo.neo4j.Path'
We can extend an complicate the query :
theCyperCode = "START n=node(2) MATCH path =n-->friend-[?]->friend_of_friend RETURN path"
And get and Result:
cypher query result:
(2)-[:"KILLS"]->(4)-[:"STEALS_FROM"]->(5)
<class 'py2neo.neo4j.Path'>
אין תגובות:
הוסף רשומת תגובה