neo4j - 出现次数最多的一对演员
问题描述
Neo4j/Cypher 用户首次尝试使用电影图示例。
我想归还一起演过最多电影的那对演员。我正在尝试的代码似乎以 DESC 顺序为我提供了我想要的东西,但我如何限制只有最高强度而不是所有对?
MATCH (n)-[:ACTED_IN]->(m)<-[:ACTED_IN]-(coActors)
RETURN n.name, coActors.name, count(*) AS Strength ORDER BY Strength DESC
解决方案
这样的事情怎么样
// find pairs of actors that acted inthe same movies together
MATCH (n1)-[:ACTED_IN]->(m)<-[:ACTED_IN]-(n2)
// ensure you only get the duo in a single ordered pair
WHERE id(n1) < id(n2)
// order them by the most prolific pairings
WITH n1, n2, count(m) AS strength
ORDER BY strength DESC
// collect all of the duos
WITH collect({actor1: n1.name, actor2: n2.name, strength: strength} ) AS duos
// find the most prolific number from the first element
WITH duos, duos[0].strength AS max_strength
// filter the collection so only those that are the most prolific are returned
RETURN [duo IN duos WHERE duo.strength = max_strength | duo] AS top_duos
推荐阅读
- android - Android Studio 中的 Webview 上没有可用的预览
- c# - WCF Web服务反序列化操作请求消息正文时出错
- javafx - 如何将 javafx 应用程序部署为 Windows 根文件夹中没有很多 dll 的图像包
- django - 文件已上传,但 API 中显示的 URL 不正确
- php - 如何使用 symfony 3.4 设置请求的语言环境
- javascript - vue-datepicker 在之前和之后禁用
- java - 尝试从 Java 代码中使用 Rest Service 时出现 503 Service Unavailable Error | 从邮递员那里工作时工作正常
- kubernetes - 'kubectl get namespace' 命令“命名空间被禁止”错误
- rx-swift - 我想在点击按钮时检查文本验证
- node.js - 如何编写像中间件这样的函数但可以有一些参数