简体   繁体   中英

Using py2neo to get nodes with second order connections?

How can py2neo RelationshipMatcher or similar be used to return all nodes with a second order connection to an original node?

I can use the following Cypher query:

MATCH (u)-[:has]-()-[:validates]-(result)
WHERE u.UserName = "Dave" 
RETURN result

Which with the below graph would give me routes A, B and C


Graph image

However, using db.evaluate(query (as below) with the same query only returns the first matching node (ie Route A )

from py2neo import Graph, Node, Relationship, NodeMatcher, RelationshipMatcher

def get_routes(username):
    query = "MATCH (u)-[:has]-()-[:validates]-(result) WHERE u.UserName = '"'{}'"' RETURN result".format(username)
    result = db.evaluate(query)

db = Graph("bolt://X.X.X.X:7687", username = "neo4j", password = "password")
get_routes("Dave")

Something like below would return the first order nodes connected to my user (ie Condition1 , Condition2 ).

How can I amend this code to deliver the matching 2nd order nodes?

u = db.nodes.match("User", UserName=username).first()
matcher = RelationshipMatcher(db)
nodes = matcher.match((u, None), "has")

Found answer here

result = db.run(query).data() instead of result = db.evaluate(query)

This returns a dictionary of matching nodes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM