对最常出现的演员

时间:2019-11-21 04:03:40

标签: neo4j cypher

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

我想归还曾在最多电影中一起表演的那对演员。我正在尝试的代码似乎可以按DESC顺序提供我想要的东西,但是如何将其限制为仅顶部强度而不是所有对?

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

1 个答案:

答案 0 :(得分:0)

这样的事情怎么样

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