0
votes

Im trying to find 'friend of friend' level recommendation in the given neo4j test data set, and I have come up with these two queries and Im not able to identify the bug in second query as it contains one extra node - '8' (Emil Eifrem). Please help me understand the difference. Thanks

1)

MATCH (keanu:Person {name:"Keanu Reeves"})-[:ACTED_IN]->()<-[:ACTED_IN]-(f),
    (f)-[:ACTED_IN]->()<-[:ACTED_IN]-(fof) 
WHERE NOT (keanu)-[:ACTED_IN]->()<-[:ACTED_IN]-(fof) AND fof <> keanu 
RETURN fof;

vs

2)

MATCH (keanu:Person {name:"Keanu Reeves"})-[:ACTED_IN]->(movie)<-[:ACTED_IN]-(f),
    (f)-[:ACTED_IN]->(f_movies)<-[:ACTED_IN]-(fof) 
WHERE NOT (movie)<-[:ACTED_IN]-(fof) AND fof <> keanu
RETURN fof;
1
version : Neo4j 2.1.2user3865770
I don't have the sample data in front of me. Is there some film that both Emil and Keanu acted in? One difference between your WHERE clauses is that the first excludes any foaf that acted in anything together with Keanu, while the second one excludes any foaf that only acted in movies with Keanu. (If there are more than one results for (movie), then it's sufficient that one of foaf pass the condition in the second query to make it into the result: or, in other words, an actor may be match as foaf several times and only the instances that don't pass the condition are excluded.)jjaderberg

1 Answers

1
votes

The movie you are referring to in the second WHERE clause is one concrete movie that was matched in the MATCH clause before, so you only check against that single movie and not all potential movies between the fof and Keanu Reeves. That's why you filter out fewer people.