Neo4j cypher:在detph 2处找到节点,没有圆形路径

时间:2014-07-02 12:02:06

标签: neo4j cypher

我想找到与当前节点距离为2的所有节点:

eg:
1<->2
2<->3
1->3
2->4

来自节点1的这种类型的A应该找到节点4

我已经尝试过这个查询,但它正在受到循环路径的影响:

start n=node({startid})
match n--> m
with distinct m as f1
match f1-->m
with distinct m as f2
return count(f2)

实际上,它在距离2处也发现1,2,3,4为节点,而不考虑1应该在距离0,2,3距离1,而只有4在距离2处。

任何建议?

1 个答案:

答案 0 :(得分:0)

你的意思是这样的:

START n=node({startid})
MATCH (n)-[*..2]->m
RETURN m

在*之后,您可以定义路径的长度。 * .. 2表示:长度介于0和2之间。

START n=node({startid})
MATCH (n)-[*2]->m
WHERE n <> m
RETURN m

对于修复长度2,WHERE将确保不返回n。

完整文档: http://docs.neo4j.org/chunked/milestone/query-match.html#match-variable-length-relationships