首页 > 解决方案 > Use neo4j cypher to find most connected nodes that meet given criteria for connection. eg. authors who co-author books

问题描述

Can anyone help with this odd twist on an otherwise standard query...

Say I have a collection of books, each has a relation to their respective authors.

I want to find the authors who have collaborated the most (and the books on which they have collaborated), where the count of collaborations excludes authors who are only connected to one book.

eg.

The result on the query above should be.

Note:

I can easily return a list of books and authors where there is more than 2 authors on book, but I am having trouble keeping either books or authors with only a single connection out of the result.

标签: neo4jcypher

解决方案


MATCH (c:Author)-[r:AUTHORED]->(:Book) WITH c, COUNT(r) as bookCount WHERE bookCount > 1 WITH COLLECT(c) as validAuthors MATCH (a1:Author)-[:AUTHORED]->(b:Book)<-[:AUTHORED]-(a2:Author) WHERE a2 IN validAuthors AND a1 in validAuthors RETURN a1.name as author, COUNT(DISTINCT b) as coAuthoredBookCount ORDER BY coAuthoredBookCount DESC

编辑:validAuthors 中的 a1 和 a2


推荐阅读