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

时间:2015-02-25 06:53:50

标签: neo4j cypher

我正在描述Facebook的一小部分,这是Neo4j中的共同朋友计算与图形表示。我希望与所有初学者分享,并对图形数据库感兴趣,特别是我的小知识neo4j。

1 个答案:

答案 0 :(得分:1)

计算两者之间相互朋友的Facebook表示示例:

  • Cypher Query代表朋友数据库:

    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);
    
  • 等效图表示: Mutual Friend Representation

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

     MATCH (Keanu:`Person` {name: "Keanu Reeves"})-[r]->(mutualFriend)<-[r1]-(Kevin:`Person` {name: "Kevin Bacon"}) RETURN mutualFriend.name
    
  • Cypher查询返回与其中一个相同数量的共同朋友 基努:

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