使用Neo4j的Cypher返回我的朋友和朋友的朋友 [英] Return my friends and friends of friends using Neo4j's Cypher
问题描述
我有两个具有双向关系的节点(如下)
I have nodes with bi-relations(follow)
我正在尝试创建一个查询,该查询要返回特定节点的所有后续节点以及他的后续节点的后续节点(3个希望深度)
I am trying to create a query which I want to return all following nodes of a specific node and the following nodes of his following nodes(3 hopes depth)
例如,假设这些关系:
->符号紧随其后
A->B
B->A
B->C
C->B
C->D
D->C
C->E
E->C
如果我在节点A上执行查询,我希望得到此响应
I am expecting to get this response if I execute the query on node A
B C d E
我尝试过:
MATCH (user:User {name:'roi'})-[:nearby*1..3]->(foaf)
WHERE NOT((user)-[:nearby]->(foaf))
RETURN user, foaf
问题是我没有让(A)节点跟随他的下列朋友
the problem is that I dont get the node (A) followings only the followings of his followings friends
我知道了
C d E
我没有得到 B
有人可以帮忙吗? 谢谢
anyone could help? thanks
推荐答案
您不会得到B
,因为您的WHERE
模式会过滤掉[:NEARBY]
到A
的任何节点.对于那种特殊的关系,就像说
You don't get B
because your WHERE
pattern filters out any node that is [:NEARBY]
to A
. For that particular relationship it is like saying
MATCH A-[:NEARBY]->B
WHERE NOT(A-[:NEARBY]->B)
修改
如果您希望用户的朋友和朋友的朋友深入三层而不是该用户,您可以这样做
If you want a users' friends and friends of friends to depth three but not the user you can do
MATCH (user:User {name:'roi'})-[:NEARBY*1..3]->f
WHERE f <> user
RETURN f
这篇关于使用Neo4j的Cypher返回我的朋友和朋友的朋友的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!