问题
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