首页 > 解决方案 > 为什么neo4j中的父节点和子节点之间存在链接

问题描述

1.我创建了 Tweet 和 TweetLeaf 节点,然后使用以下代码创建 realatioships

MERGE (n:Tweet {id:'13084664', title:'A'})

MERGE (cl1:TweetLeaf {id:'13085170', title:'AB', reply_to:'13084664'})

MERGE (cl2:TweetLeaf {id:'13085179', title:'TESTING Team', reply_to:'13085170'})

MERGE (cl2:TweetLeaf {id:'13085352', title:'TESTING Team', reply_to:'13085170'})

MERGE (cl4:TweetLeaf {id:'13085443', title:'TESTING Team', retweet_to:'13085352'})

MERGE (cl5:TweetLeaf {id:'13085356', title:'TESTING Team', retweet_to:'13085352'})

//parent and leaf.....
MATCH (parent:Tweet)
MATCH (c:TweetLeaf)
WHERE NOT (c)-[:reply]->() 
AND parent.id = c.reply_to
WITH parent, c
MERGE (c)-[:reply_to]->(parent)
//RETURN c, parent

//leaf to leaf reply_to.....
MATCH (c1:TweetLeaf)-[]-()
WHERE exists (c1.reply_to)

MATCH (d1:TweetLeaf)
WHERE NOT (d1)-[]->() and exists (d1.reply_to)
AND c1.id = d1.reply_to

WITH c1, d1
MERGE (d1)-[:reply_to]->(c1)
//RETURN c1, d1

//leaf to leaf retweet_to....
MATCH (c2:TweetLeaf)
WHERE exists (c2.retweet_to)
AND NOT (c2)-[]->()

MATCH (d2:TweetLeaf)-[]->()
WHERE d2.id = c2.retweet_to

WITH c2, d2
MERGE (c2)-[:retweet_to]->(d2)
//RETURN c2, d2

2.最终结果应该像下面的截图 在此处输入图像描述

但是,在我的输出中,节点 A 和节点 AB 之间没有链接。屏幕截图显示了我的输出图 在此处输入图像描述

标签: neo4jcypher

解决方案


您用于生成可视化的查询显然只返回TweetLeaf节点,因此 neo4j 浏览器只显示这些节点。

相反,请尝试使用此查询(它返回涉及TweetLeaf节点的所有长度为 0 或 1 的路径):

MATCH p=(:TweetLeaf)-[*0..1]-()
RETURN p

如果有独立TweetLeaf节点,长度为 0 的路径将没有关系。如果您不关心这种边缘情况,那么您可以p=(:TweetLeaf)--()改用。


推荐阅读