Recursive Cypher query following relations with a given direction and property

cypher

(Michael Horak) #1

Hello,

I would like to ask if it is possible to write a Cypher query that returns a path with the following conditions.
I have an id of the starting node, then I want to follow all outgoing relations with property t=2 and all incoming relations with a property t=4. All this should be recursive so it stops when there are no more outgoing relations with property t=2 and incoming with property t=4.

For now I found the following query

MATCH p=(n0 {id:1234})<-[*0..]-(n) WHERE all(r IN relationships(p) WHERE r.t = 4            
   	RETURN p 
UNION
MATCH p=(n0 {id:1234})-[*0..]->(n) WHERE all(r IN relationships(p) WHERE r.t = 2 
	"RETURN p;

but it does not work for cases like

eg. (n0)-[{t=2}]->(n1)<-[{t=4}]-(n2)<-[{t=4}]-(n3)-[{t=2}]->(n4) => in this case I should get all the path from n0 untill n4

Thank you,

Michael