Neo4j Coactor网络以及网络中Coactor之间的电影计数

Neo4j Coactor网络以及网络中Coactor之间的电影计数,neo4j,cypher,graph-databases,Neo4j,Cypher,Graph Databases,我想创建一个简单的coactor网络(来自neo4j的电影数据库),它返回coactor名称和coactor中的电影计数,作为degree。我试图通过两个查询来实现它 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 degre

我想创建一个简单的coactor网络(来自neo4j的电影数据库),它返回coactor名称和coactor中的电影计数,作为degree。我试图通过两个查询来实现它

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 |
    +----------------+----------------------+--------+

任何正确方向的见解都将受到高度赞赏。谢谢。

像这样的方法应该可以:

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
我们需要首先收集基努·里夫斯和他的所有合作者,然后将他们作为一个单一变量进行处理,并且只匹配该集合中的合作者


请记住,此处的结果包括镜像结果(因此,您将在一行上显示
“基努·里夫斯”“凯莉·安妮·莫斯”
,在另一行上显示
“凯莉·安妮·莫斯”“基努·里夫斯”
)。如果你想解决这个问题,你需要在WHERE子句上添加一个谓词,应用某种限制,比如
和actor.name
或者
和id(actor)

我已经添加了谓词
和id(actor)
,查询就像一个没有镜像结果的符咒一样工作。非常感谢。
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