首页 > 解决方案 > 出现次数最多的一对演员

问题描述

Neo4j/Cypher 用户首次尝试使用电影图示例。

我想归还一起演过最多电影的那对演员。我正在尝试的代码似乎以 DESC 顺序为我提供了我想要的东西,但我如何限制只有最高强度而不是所有对?

MATCH (n)-[:ACTED_IN]->(m)<-[:ACTED_IN]-(coActors)
RETURN n.name, coActors.name, count(*) AS Strength ORDER BY Strength DESC

标签: neo4jcypher

解决方案


这样的事情怎么样

// 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

推荐阅读