简体   繁体   中英

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: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?

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.

If you only want the shortest path between these two, you can match to both then match using the shortestPath function:

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

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