H. Trujillo
H. Trujillo

Reputation: 437

Return multiple relationships as one in cypher

Consider a query where to get from node A to node D you must travel through a couple of relationships, ex.

(:Person)-[:LIVES_IN]->(:State)-[:HAS]->(:Parks)

and I wanted to return on the fly all the possible Parks a Person can go to in their State, without returning the State itself in the query such that the returned relationship looks like

(:Person)-->(:Parks)

In other words so that:

enter image description here

What would the query look like?

Thank you for your time!

Upvotes: 3

Views: 2209

Answers (2)

Dave Bennett
Dave Bennett

Reputation: 11216

You could use APOC to return a virtual relationship something like this...

MATCH (person:Person {name: 'Dave'})-[:LIVES_IN|HAS*]->(park:Park)
WITH person, park
CALL apoc.create.vRelationship(person,'CAN_VISIT',{}, park) YIELD rel
RETURN person, park, rel

Upvotes: 3

Fabio Lamanna
Fabio Lamanna

Reputation: 21552

If you want to get all the Parks a Person can go to you can simply query for:

MATCH (:Person)-[*]->(p:Parks)
RETURN p

Upvotes: 2

Related Questions