0
votes

I wrote 2 similar queries, intended to find "any actor who co-acted with actors that co-acted a movie that Tom Hanks acted, but not an co-actor of a Tom Hanks movie".

I thought I can use movie1.title <> movie2.title to be the condition, however the result is different

Query 1 (answer provided on Neo4j.com):

MATCH
  (p:Person)-[:ACTED_IN]->(m:Movie)<-[:ACTED_IN]-(p2:Person {name:"Tom Hanks"}),
  (candidate:Person)-[:ACTED_IN]->(m2:Movie)<-[:ACTED_IN]-(p:Person)
where NOT (p2)-[:ACTED_IN]->()<-[:ACTED_IN]-(candidate)
return candidate,m2.title,count(*) as strong order by strong DESC

My Query:

MATCH
  (p:Person)-[:ACTED_IN]->(m)<-[:ACTED_IN]-(p2:Person {name:"Tom Hanks"}),
  (candidate:Person)-[:ACTED_IN]->(m2)<-[:ACTED_IN]-(p:Person)
where m.title<>m2.title and p2<>candidate
return candidate.name ,count(*) as strong order by strong DESC

Anyone can very briefly explain why my query result contains more actors?

1

1 Answers

1
votes

Suppose candidate and Tom Hanks co-starred in some movie other than m and m2. The m.title<>m2.title test would pass, and candidate would be returned.

You have to ensure that there are no movies at all in which they co-starred together. That is what the first query tests.

Aside: the m.title<>m2.title test should have been m<>m2 instead, since different movies can have the same title.