Neo4j Coactor 网络以及网络中 Coactors 中的电影计数
Neo4j Coactor network along with movie count among coactors in the network
我想创建一个简单的共同角色网络(来自 neo4j 的电影数据库),其中 returns 共同角色名称和电影计数 在共同角色中 。我试图通过两个查询来实现它
First query
MATCH (a:Person {name:'Keanu Reeves'})-[:ACTED_IN]->(movie)<-[:ACTED_IN]-(coactor) RETURN a.name,coactor.name,count(movie) as degree ORDER BY degree DESC
Output from first query
+----------------+----------------------+--------+
| a.name | coactor.name | degree |
+----------------+----------------------+--------+
| "Keanu Reeves" | "Hugo Weaving" | 3 |
| "Keanu Reeves" | "Laurence Fishburne" | 3 |
| "Keanu Reeves" | "Carrie-Anne Moss" | 3 |
| "Keanu Reeves" | "Diane Keaton" | 1 |
| "Keanu Reeves" | "Jack Nicholson" | 1 |
| "Keanu Reeves" | "Brooke Langton" | 1 |
+----------------+----------------------+--------+
Second query
MATCH (a1:Person {name:'Hugo Weaving'})-[:ACTED_IN]->(movie)<-[:ACTED_IN]-(a2:Person {name:'Laurence Fishburne'}) RETURN a1.name,a2.name,count(movie) as degree ORDER BY degree DESC
Output from second query
+----------------+----------------------+--------+
| a1.name | a2.name | degree |
+----------------+----------------------+--------+
| "Hugo Weaving" | "Laurence Fishburne" | 3 |
+----------------+----------------------+--------+
我想对我从第一个查询中获得的所有 coactors 执行第二个查询 。
我的想法是拥有一个共同角色列表,并通过在循环中重复第二个查询并将结果附加到第一个查询输出来获取每对共同角色的电影计数?
还有其他有效的方法吗?有什么办法可以在一次查询中获取所有信息吗?还是我在这里做错了什么。
预期结果
第一个查询的输出 + 第二个查询的输出
+----------------+----------------------+--------+
| a.name | coactor.name | degree |
+----------------+----------------------+--------+
| "Keanu Reeves" | "Hugo Weaving" | 3 |
| "Keanu Reeves" | "Laurence Fishburne" | 3 |
| "Keanu Reeves" | "Carrie-Anne Moss" | 3 |
| "Keanu Reeves" | "Diane Keaton" | 1 |
| "Keanu Reeves" | "Jack Nicholson" | 1 |
| "Keanu Reeves" | "Brooke Langton" | 1 |
| "Hugo Weaving" | "Laurence Fishburne" | 3 |
| Carrie-Anne Moss| Laurence Fishburne | ? |
+----------------+----------------------+--------+
任何正确方向的见解都将受到高度赞赏。谢谢。
像这样的东西应该可以工作:
MATCH (k:Person {name:'Keanu Reeves'})-[:ACTED_IN*2]-(coactor)
WITH k + collect(DISTINCT coactor) as coactors
UNWIND coactors as actor
MATCH (actor)-[:ACTED_IN*2]-(coactor)
WHERE coactor IN coactors
WITH actor, coactor, count(*) as degree
ORDER BY degree DESC
RETURN actor.name, coactor.name, degree
我们需要首先收集基努·里维斯和他所有的共同演员,然后将它们作为一个变量来处理,并且只匹配该集合中的共同演员。
请记住,此处的结果包括镜像结果(因此您将在一行上有 "Keanu Reeves" "Carrie-Anne Moss"
,在另一行上有 "Carrie-Anne Moss" "Keanu Reeves"
)。如果你想解决这个问题,你需要在你的 WHERE 子句上添加一个谓词,应用某种限制,比如 AND actor.name < coactor.name
或 AND id(actor) < id(coactor)
我想创建一个简单的共同角色网络(来自 neo4j 的电影数据库),其中 returns 共同角色名称和电影计数 在共同角色中 。我试图通过两个查询来实现它
First query
MATCH (a:Person {name:'Keanu Reeves'})-[:ACTED_IN]->(movie)<-[:ACTED_IN]-(coactor) RETURN a.name,coactor.name,count(movie) as degree ORDER BY degree DESC
Output from first query
+----------------+----------------------+--------+
| a.name | coactor.name | degree |
+----------------+----------------------+--------+
| "Keanu Reeves" | "Hugo Weaving" | 3 |
| "Keanu Reeves" | "Laurence Fishburne" | 3 |
| "Keanu Reeves" | "Carrie-Anne Moss" | 3 |
| "Keanu Reeves" | "Diane Keaton" | 1 |
| "Keanu Reeves" | "Jack Nicholson" | 1 |
| "Keanu Reeves" | "Brooke Langton" | 1 |
+----------------+----------------------+--------+
Second query
MATCH (a1:Person {name:'Hugo Weaving'})-[:ACTED_IN]->(movie)<-[:ACTED_IN]-(a2:Person {name:'Laurence Fishburne'}) RETURN a1.name,a2.name,count(movie) as degree ORDER BY degree DESC
Output from second query
+----------------+----------------------+--------+
| a1.name | a2.name | degree |
+----------------+----------------------+--------+
| "Hugo Weaving" | "Laurence Fishburne" | 3 |
+----------------+----------------------+--------+
我想对我从第一个查询中获得的所有 coactors 执行第二个查询 。
我的想法是拥有一个共同角色列表,并通过在循环中重复第二个查询并将结果附加到第一个查询输出来获取每对共同角色的电影计数?
还有其他有效的方法吗?有什么办法可以在一次查询中获取所有信息吗?还是我在这里做错了什么。
预期结果
第一个查询的输出 + 第二个查询的输出
+----------------+----------------------+--------+
| a.name | coactor.name | degree |
+----------------+----------------------+--------+
| "Keanu Reeves" | "Hugo Weaving" | 3 |
| "Keanu Reeves" | "Laurence Fishburne" | 3 |
| "Keanu Reeves" | "Carrie-Anne Moss" | 3 |
| "Keanu Reeves" | "Diane Keaton" | 1 |
| "Keanu Reeves" | "Jack Nicholson" | 1 |
| "Keanu Reeves" | "Brooke Langton" | 1 |
| "Hugo Weaving" | "Laurence Fishburne" | 3 |
| Carrie-Anne Moss| Laurence Fishburne | ? |
+----------------+----------------------+--------+
任何正确方向的见解都将受到高度赞赏。谢谢。
像这样的东西应该可以工作:
MATCH (k:Person {name:'Keanu Reeves'})-[:ACTED_IN*2]-(coactor)
WITH k + collect(DISTINCT coactor) as coactors
UNWIND coactors as actor
MATCH (actor)-[:ACTED_IN*2]-(coactor)
WHERE coactor IN coactors
WITH actor, coactor, count(*) as degree
ORDER BY degree DESC
RETURN actor.name, coactor.name, degree
我们需要首先收集基努·里维斯和他所有的共同演员,然后将它们作为一个变量来处理,并且只匹配该集合中的共同演员。
请记住,此处的结果包括镜像结果(因此您将在一行上有 "Keanu Reeves" "Carrie-Anne Moss"
,在另一行上有 "Carrie-Anne Moss" "Keanu Reeves"
)。如果你想解决这个问题,你需要在你的 WHERE 子句上添加一个谓词,应用某种限制,比如 AND actor.name < coactor.name
或 AND id(actor) < id(coactor)