neo4j - 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.
- Author1 has authored 3 books (book A, B, C and D) and would therefore be eligible to be counted as a co-author. Note book A is solo authored by author 1
- Author2 has only one book(book b) and would therefore not be eligible to be counted as a co-author.
- Author 3 has authored 2 books (book b & c) and can be counted.
- Author 4 has authored 2 books (book C & D) and can be counted.
The result on the query above should be.
- Author 1 - co-authorship count 2
- Author 3 - co-authorship count 1
- Author 4 - co-authorship count 1
- Book B
- Book c
- Book D
Note:
- Author 2 would be ineligble (only co-authored a single book)
- Book A would be ineligble (there is only one author)
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.
解决方案
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
推荐阅读
- python - 在python中使用dlib保存带有面部标志的图像?
- javascript - 从数据库添加项目到列表并在提交后清除输入
- mysql - lastInsertId(tableName_id_seq) 返回:未定义表:7 错误:关系“tableName_id_seq”不存在
- javascript - 我将如何根据图像是否已加载或无法加载来更改段落的内容?
- c# - NWebSec 在发布模式下重定向到 HTTPS
- edit - 我可以在启动屏幕 xcode 中使用渐变颜色吗
- bash - 在 RaspberyPi 中仅下载新字幕
- ios - 用于 Mac 上 Windows 的 Cordova 插件添加/删除
- sql - 一次循环 10 条记录并将其分配给变量
- android - 如何使用 Mapbox SDK 检测过期的身份验证令牌?