cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Yet Another Repeat Until Question (YARUQ)

I want to return all nodes/relationships until I find the first node that has a certain relationship in an arbitrary number of iterations.

Example

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

1 ACCEPTED SOLUTION

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...

View solution in original post

2 REPLIES 2

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...

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...