Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
12-09-2020 07:53 AM
I want to return all nodes/relationships until I find the first node that has a certain relationship in an arbitrary number of iterations.
Given:
X(1)-[foo]->X(2)-[foo]->X(3)-[bar]->X(4)-[foo]->X(5)
I want this returned:
X(1)-[foo]->X(2)-[foo]->X(3)-[bar]->X(4)
This does NOT work:
MATCH (f:X), r=(f)-[:foo|bar*1..]->(t:X)
WHERE id(f) = 1 AND NOT ()-[:bar]->(f)
WITH f, relationships(r) AS rs, nodes(r) AS ns
RETURN f, rs, ns
It returns the entire graph:
X(1)-[foo]->X(2)-[foo]->X(3)-[bar]->X(4)-[foo]->X(5)
Is this because of the 2 WHERE conditions being temporally at odds?
How do I change it to work?
In addition, I'd like to filter on the ID of the last traversed relationship is in the terminating condition.
like (yes, this is invalid, silly syntax):
MATCH (f:X), r=(f)-[:foo|bar*1..]->(t:X)
WHERE id(f) = 1 AND NOT (f-1)-[:bar]->(f)
WITH f, relationships(r) AS rs, nodes(r) AS ns
RETURN f, rs, ns
Is this all too complex for Cypher??
Solved! Go to Solution.
12-09-2020 09:22 AM
OK, here's the second answer:
MATCH (f:X {Id:1}), x=(f)-[:foo|bar*1..]->(t:X)
WITH x, relationships(x) AS r
WITH x, startNode(r[size(r)-1]) AS p1, startNode(r[size(r)-2]) AS c1
WHERE NOT (c1)-[:bar]->(p1)
RETURN x
Whew! Now that's a lot of Cyphering...
12-09-2020 08:42 AM
Figured it out the first question myself after all:
MATCH (f:X {Id:1}), x=(f)-[:foo|bar*1..]->(t:X)
WITH x, startNode(last(relationships(x))) AS s
WHERE NOT ()-[:bar]->(s)
RETURN x
Someone can still answer whether (f-1) is possible somehow...
These 'functions' look promising..maybe if I can index into the list of x's rels...
12-09-2020 09:22 AM
OK, here's the second answer:
MATCH (f:X {Id:1}), x=(f)-[:foo|bar*1..]->(t:X)
WITH x, relationships(x) AS r
WITH x, startNode(r[size(r)-1]) AS p1, startNode(r[size(r)-2]) AS c1
WHERE NOT (c1)-[:bar]->(p1)
RETURN x
Whew! Now that's a lot of Cyphering...
All the sessions of the conference are now available online