0
votes

so I'm following this guide/exercise on neo4j about a movie database, where I'm also using the same data from there(link here). Now my problem is trying to find the co-co actors of a particular person. eg Tom Hanks. co co actors are all the actors who have acted in a movie with a co-actor of Tom Hanks, but have not acted in a movie with Hanks Now looking at the solutions for exercise 5.4 from the link, my code should look something like this right? I run the code, but there's no output/records

MATCH (a:Actor)-[:ACTED_IN*2]->(a2:Actor)    
WHERE a.name = "Tom Hanks"
AND NOT((a)-[:ACTED_IN]-(a2))
RETURN a2.name AS `co-co-actors`

or maybe like this?

MATCH (a:Actor)-[:ACTED_IN]->(m:Movie)<-[:ACTED_IN]-(a2:Actor), 
    (a2:Actor)-[:ACTED_IN]->(m:Movie)<-[:ACTED_IN]-(a3:Actor)
WHERE a.name = 'Tom Hanks'
RETURN a3.name AS `co-co-actors`
1

1 Answers

1
votes

You're close! First, you can indeed fetch the co-actors of Tom Hanks:

(tom:Person {name: "Tom Hanks"})-[:ACTED_IN]->(:Movie)<-[:ACTED_IN]-(coActor:Person)

Then, you want to find the co-actors of Tom Hanks co-actors:

(coCoActor:Person)-[:ACTED_IN]->(:Movie)<-[:ACTED_IN]-(coActor)

But you want to make sure that these "co-co-actors" never played in any movie with Tom Hanks:

WHERE NOT (coCoActor)-[:ACTED_IN]->(:Movie)<-[:ACTED_IN]-(tom)

The whole query becomes then:

MATCH (tom:Person {name: "Tom Hanks"})-[:ACTED_IN]->(:Movie)<-[:ACTED_IN]-(coActor:Person),
      (coCoActor:Person)-[:ACTED_IN]->(:Movie)<-[:ACTED_IN]-(coActor)
WHERE NOT (coCoActor)-[:ACTED_IN]->(:Movie)<-[:ACTED_IN]-(tom)
RETURN coCoActor.name AS `co-co-actor`

For instance, Jack Nicholson seems to be a co-co-actor of Tom Hanks. Let's check:

MATCH (tom:Person {name: "Tom Hanks"}), (jack:Person {name: "Jack Nicholson"})
RETURN shortestPath((tom)-[*]-(jack))

shortest path between Tom Hanks and Jack Nicholson

It seems to be the case, hurray!