Return nodes who are connected by a common set of nodes

ⅰ亾dé卋堺 提交于 2019-12-24 07:37:15

问题


Is there a way in Neo4j, using either cypher or gremlin, to return a list of nodes that have a common set of nodes between them?

An example would be

Person1-[KNOWS]->Friend1
Person1-[KNOWS]->Friend2
Person1-[KNOWS]->Friend3

Person2-[HATES]->Friend2
Person2-[HATES]->Friend3

I want to start at Person1 and say, "Find me the people who hate all the people I know", which should return Person2 since Person1 knows Friend2,Friend3 and Person2 hates Friend2,Friend3.

I've started by finding the connection,

START
    person=node(1)
MATCH
    person-[KNOWS]->friend<-[HATES]-enemy
RETURN 
    enemy

but I can't seem to find a way to express it such that the Person has to hate ALL the friends.

Can this be done in Cypher?


回答1:


the syntax should be as follows, but i can't get rid off the aggregate error message

START
    person=node(1)
MATCH
    person-[r1:KNOWS]->friend<-[r2:HATES]-enemy
WHERE
    count(distinct r1)=count(distinct r2)
RETURN 
    enemy

edit: maybe this is closer:

START
    person=node(1)
MATCH
    person-[r1:KNOWS]->friend<-[r2:HATES]-enemy, person-[r3?:KNOWS]-enemy
WITH
    person, enemy, count(distinct r1) as  rk1, count(distinct r2) as rk2,r3
WHERE 
    r3 is null
    AND
    r1=r2 
RETURN 
    enemy


来源:https://stackoverflow.com/questions/11182830/return-nodes-who-are-connected-by-a-common-set-of-nodes

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