Match nodes with common nodes with a relationship - Neo4j Cypher

霸气de小男生 提交于 2019-12-02 09:47:24
Gabor Szarnyas

Here is an adaptation of my answer for a similar question:

MATCH (u1:User)-[:KNOWS]->(:Skill)<-[:KNOWS]-(u2:User) // (1)
MATCH
  (u1)-[:KNOWS]->(s1:Skill),
  (u2)-[:KNOWS]->(s2:Skill) // (2)
WITH
  u1, u2, 
  COUNT(DISTINCT s1) AS s1Count, COUNT(DISTINCT s2) AS s2Count // (3)
MATCH (u1)-[:KNOWS]->(s:Skill)<-[:KNOWS]-(u2) // (4)
WITH
  u1, u2,
  s1Count, s2Count, COUNT(s) AS commonSkillsCount // (5)
WHERE
  // we only need each u1-u2 pair once
  ID(u1) < ID(u2) AND // (6)
  // similarity
  commonSkillsCount / 0.5 >= s1Count AND
  commonSkillsCount / 0.5 >= s2Count // (7)
RETURN u1, u2
ORDER BY u1.name, u2.name

We look for u1, u2 Users that have at least a single common Skill (1). We then gather their individual skills separately (2) and count them (3), and also gather their mutual Skills (4) and count them (5). We then remove one of the (u1, u2) and (u2, u1) pairs (e.g. from (Alice, Bob) and (Bob, Alice) we only keep the former (6) and check if the number of their common skills is above the threshold (7). (Floating point arithmetics is sometimes tricky in Cypher - IIRC if we move the / 0.5 to the right side of the inequality as sXCount * 0.5, we'd have to use the toFloat() function).

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