简体   繁体   English

我将如何查询这两个节点之间的关系?

[英]How would I query for a relationship between these two nodes?

If I have a graph that looks like so for nodes (p) and (e): 如果我有一个节点(p)和(e)像这样的图:

(p:Person)-[r:WorksFor]->(e:Employer)

And I have the following data: 我有以下数据:

(Person {name: Andrew})-[r:WorksFor]->(Employer {name: Google})
(Person {name: James})-[r:WorksFor]->(Employer {name: Google})
(Person {name: James})-[r:WorksFor]->(Employer {name: Apple})
(Person {name: Evan})-[r:WorksFor]->(Employer {name: Apple})

How can I query between (Person {name: Evan}) through each relationship and get to (Person {name: Andrew}) returning each employer and person along the way with an arbitrary number of employers and persons in-between? 如何通过每种关系在(Person {name:Evan})之间进行查询,然后到达(Person {name:Andrew})之间,从而返回每个雇主和个人以及中间有任意数量的雇主和个人?

Ideally the above would return a chain that looked like: 理想情况下,上面的代码将返回如下所示的链:

(Andrew)->(Google)->(James)->(Apple)->(Evan)

Thank you for your help. 谢谢您的帮助。

(EDIT) Addendum: (编辑)附录:

The following seems to work but only if the players are separated by only two degrees, is there a way to make this completely variable length? 以下内容似乎可行,但仅当玩家分开两个角度时,有没有办法使长度完全可变?

MATCH 
(p:Person {name: "Andrew"})-->(e:Employer)<--(p3:Person)-->(e2:Employer)<--(p2:Person {name: "Evan"}) 
RETURN *

You want variable-length pattern matching . 您需要可变长度的模式匹配

Depending on your graph you can define the relationships to traverse or leave off the type. 根据您的图形,您可以定义要遍历或忽略类型的关系。 We can omit the direction to specify that we don't care about the direction of relationships to traverse: 我们可以省略方向来指定我们不关心关系的遍历方向:

MATCH path = (p:Person {name: "Andrew"})-[:WorksFor*]-(p2:Person {name: "Evan"}) 
RETURN path

If you want the nodes in the path, you can return nodes(path) to get that list. 如果要在路径中包含节点,则可以返回nodes(path)以获取该列表。

If you only want the shortest path between these two, you can match to both then match using the shortestPath function: 如果只需要这两者之间的最短路径,则可以将两者匹配,然后使用shortestPath函数进行匹配:

MATCH (p:Person {name: "Andrew"}), (p2:Person {name: "Evan"}) 
MATCH path = shortestPath((p)-[:WorksFor*]-(p2))
RETURN path

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

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