如何计算节点的不同计数

how to calculate a distinct count for nodes

我在 neo4j 项目中需要你的帮助。我有两个节点作者和文章。它们之间的关系是

(author:Author)-[:WRITES]->(article:Article)

一篇文章可以由多个作者撰写。所以我想计算哪些是合作最多的前 5 位作者(与不同的作者)。另外,我想 return 作者姓名和合作次数。我尝试了以下但没有用。

MATCH (article:Article)<-[:WRITES]-(author:Author)
with article, collect(distinct author.name) as authors
RETURN authors,size(authors)-1 as numberofcollaborations
ORDER BY numberofcollaborations DESC
LIMIT 5; 

有什么想法吗?

您可以使用路径模式获取每篇文章的贡献者,然后按作者汇总:

MATCH (author:Author)-[:WRITES]->(article:Article)<-[:WRITES]-(coauthor:Author)
WITH author, 
     size(collect(distinct coauthor)) as numberofcollaborations 
     ORDER BY numberofcollaborations 
     DESC LIMIT 5
RETURN author.name as author, 
       numberofcollaborations