如何根据 facebook 表示在 Neo4j 中使用密码查询计算共同好友?

How to calculate mutual friend using cypher query in Neo4j as per facebook representation?

我正在描述facebook的一小部分,它是Neo4j中的共同好友计算,具有图形表示。我想用我的一点知识与所有初学者和对图数据库特别是neo4j感兴趣的人分享它。

计算两者之间共同好友的 Facebook 表示示例:

  • 表示好友数据库的密码查询:

    CREATE (Keanu:Person {name:'Keanu Reeves', born:1964})
    CREATE (Carrie:Person {name:'Carrie-Anne Moss', born:1967})
    CREATE (Laurence:Person {name:'Laurence Fishburne', born:1961})
    CREATE (JoelS:Person {name:'Joel Silver', born:1952})
    CREATE (DemiM:Person {name:'Demi Moore', born:1962})
    CREATE (KevinB:Person {name:'Kevin Bacon', born:1958})
    CREATE (KieferS:Person {name:'Kiefer Sutherland', born:1966})
    CREATE (NoahW:Person {name:'Noah Wyle', born:1971})
    CREATE
       (Keanu)-[:FRIENDS_WITH ]->(Carrie),
       (Keanu)-[:FRIENDS_WITH ]->(Laurence),
       (Keanu)-[:FRIENDS_WITH ]->(JoelS),
       (Keanu)-[:FRIENDS_WITH ]->(DemiM),
       (KevinB)-[:FRIENDS_WITH ]->(KieferS),
       (KevinB)-[:FRIENDS_WITH ]->(NoahW),
       (KevinB)-[:FRIENDS_WITH ]->(DemiM),
       (KevinB)-[:FRIENDS_WITH ]->(JoelS);
    
  • 等效图形表示:

  • 检索 KevinB 和 KevinB 之间共同朋友的名字的 Cypher 查询 基努:

     MATCH (Keanu:`Person` {name: "Keanu Reeves"})-[r]->(mutualFriend)<-[r1]-(Kevin:`Person` {name: "Kevin Bacon"}) RETURN mutualFriend.name
    
  • Cypher 查询 Return what Number of Mutual Friend with which one of 基努:

     MATCH (Keanu:`Person` {name: "Keanu Reeves"})-[r]->(mutualFriend)<-[r1]-(unknown) RETURN COUNT (mutualFriend) as NumberOfMutualFriend,unknown.name