Performing a MATCH on all nodes on a path

我只是一个虾纸丫 提交于 2019-12-11 03:55:38

问题


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

回答1:


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?



来源:https://stackoverflow.com/questions/13776273/performing-a-match-on-all-nodes-on-a-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!