Returned nodes that are connected by a common set of nodes

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 with Person1 and say, “Find me people who hate all the people I know” who should return Person2 , since Person1 knows Friend2,Friend3 and Person2 hates Friend2,Friend3 ,

I started by looking for a connection,

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

but I can’t find a way to express it in such a way that Man should hate ALL friends.

Can this be done in Cypher?

0
source share
1 answer

The syntax should be as follows, but I cannot get rid of the general 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 
+1
source

Source: https://habr.com/ru/post/1439858/


All Articles