Fynn
Fynn

Reputation: 4873

Performing a MATCH on all nodes on a path

Is it somehow possible to use a MATCH pattern inside the ALL function (using v1.8)?

What I am trying to do is the following: I am MATCHing a path p = (a)-->(b)-->(c)-->(d). However, all nodes along this path must have an additional incoming relationship r from some node. Let me try to make this clear in ASCII:

(a)-->(b)-->(c)-->(d)
       ^     ^     ^
       |r    |r    |r
      ( )   ( )   ( )

Can I somehow use the ALL function for that or do I have to add additional MATCH patterns like this:

START ...
MATCH (a)-->(b)-->(c)-->(d)..., ()-[:r]->(b), ()-[:r]->(c), ...
RETURN ...

Update:

Here is an example in the Neo4j console:

START n=node(0) 
CREATE (a), (b), (c), (d), (e),
n-[:rel1]->a, n-[:rel1]->b, n-[:rel1]->d, n-[:rel1]->e,
a-[:rel2]->b-[:rel3]->d, a-[:rel2]->c-[:rel3]->e

START n=node(0) 
MATCH n -[:rel1]-> x -[:rel2]-> y -[:rel3]-> z, ()-[:rel1]->y, ()-[:rel1]->z 
RETURN z

Upvotes: 2

Views: 1458

Answers (1)

Andres
Andres

Reputation: 1454

You can do this using WHERE ALL, like this:

START n=node(0) 
MATCH path = n -[:rel1]-> x -[:rel2]-> y -[:rel3]-> z 
WHERE ALL(n in tail(nodes(path)) WHERE ()-[:rel1]->n) 
RETURN z

tail(nodes(path)) returns all nodes in the path except the first one. In your example, the start node was not connected with a rel1 relationship, so nothing was returned. If you want to do it like your text explains it, just drop the tail part.

Was this what you were looking for?

Upvotes: 8

Related Questions