Reputation: 17
First time Neo4j/Cypher user experimenting with the the movie graph example.
I want to return the pair of actors that have acted together in the most number of movies. The code I'm trying appears to give me what I want in DESC order, but how do I limit to only the top Strength instead of all pairs?
MATCH (n)-[:ACTED_IN]->(m)<-[:ACTED_IN]-(coActors)
RETURN n.name, coActors.name, count(*) AS Strength ORDER BY Strength DESC
Upvotes: 1
Views: 382
Reputation: 11216
How about something like this
// 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
Upvotes: 0