简体   繁体   中英

Neo4j: query to find 2 node with the most

I'm trying to find which pair of actors have acted together in most number of movies in my data base and my query kept returning blank, any suggestions?

MATCH (actor1:Actor)<-[st1:ACTED_IN]-(mv1:Movie)-[st2:ACTED_IN]->(actor2:Actor) 
RETURN distinct actor1,actor2,count(mv1)

Looks like you have written relationship arrows in reverse direction.

It might be from Actor to Movie:

MATCH (actor1:Actor)-[st1:ACTED_IN]->(mv1:Movie)<-[st2:ACTED_IN]-(actor2:Actor) 
RETURN distinct actor1, actor2, count(mv1)

Though you are using distinct your query will return duplicates because the following two are different records:

actor1, actor2, movie_count  
actor2, actor1, movie_count

To get rid of this duplicate entries you can use simple trick of comparing ids of nodes like:

MATCH (actor1:Actor)-[:ACTED_IN]->(mv1:Movie)<-[:ACTED_IN]-(actor2:Actor) 
WHERE id(actor1)>id(actor2)
RETURN actor1,actor2,count(mv1)

To find actors acted in most movies:

MATCH (actor1:Actor)-[:ACTED_IN]->(mv1:Movie)<-[:ACTED_IN]-(actor2:Actor) 
WHERE id(actor1)>id(actor2)
RETURN actor1,actor2,count(mv1) AS movie_count 
ORDER BY movie_count DESC 
LIMIT 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM