neo4j - finding mutual friends in a cypher statement starting with three or more persons -


i trying build cypher statement neo4j know 2-n starting nodes name , need find node (if any) can reached of starting nodes.

at first tought similar "mutual friend" situation handled (start1)-[*..2]->(main)<-[*..2]-(start2) in case have more 2 starting points around 6 know name.

so puzzled how can include third, fourth , on node cypher able find commmon root amongst them.

how find anders

in above example neo4j website need path starting 'dilshad', 'becky' , 'cesar' check if have common friend (anders) excluding 'filipa' , 'emil' not friends of three.

so far create statement programmatically looks like

match (start1 {name:'person1'}), (start2 {name:'person2'}),  (start3 {name: 'person3'}), (main)  (start1)-[*..2]->(main) ,  (start2)-[*..2]->(main) ,  (start3)-[*..2]->(main) return distinct main 

but wondering if there more elegant / efficient way in cypher possibly use list of names parameter

the query shown in question building cartesian product because matching multiple disconnected patterns.

instead of match nodes separately , use where restrict relations between these nodes can like:

match (start1 {name:'person1'})-[*..2]->(main), (start2 {name:'person2'})-[*..2]->(main),  (start3 {name: 'person3'})-[*..2]->(main) return main 

the above query more efficient because match required pattern. note when doing match (start1 {name:'person1'}), (start2 {name:'person2'}), (start3 {name: 'person3'}), (main), part (main) matching nodes of graph because no restrictions specified. can use profile query see more clearly.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -