简体   繁体   English

使用py2neo获取具有二阶连接的节点?

[英]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? py2neo RelationshipMatcher或类似方法如何用于将所有具有第二级连接的节点返回到原始节点?

I can use the following Cypher query: 我可以使用以下Cypher查询:

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

Which with the below graph would give me routes A, B and C 下图将给我路线A,B和C


Graph image 图形图像

However, using db.evaluate(query (as below) with the same query only returns the first matching node (ie Route A ) 但是,对同一查询使用db.evaluate(query (如下))仅返回第一个匹配节点(即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 ). 如下所示,将返回连接到我的用户的一阶节点(即Condition1Condition2 )。

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) result = db.run(query).data()而不是result = db.evaluate(query)

This returns a dictionary of matching nodes 这将返回匹配节点的字典

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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