如何计算节点的不同计数

问题描述

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

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

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

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