简体   繁体   中英

Why do these queries in Neo4j return different results?

I am practicing on Neo4j movie database that come with it when you install Neo4j. For some reason, these two queries return different results:

match (keanu:Person {name: "Keanu Reeves"})-[:ACTED_IN]->()
<-[:ACTED_IN]-(actor), (actor)-[:ACTED_IN]->()<-[:ACTED_IN]-(other)  
where NOT ((keanu)-[:ACTED_IN]->()<-[:ACTED_IN]-(other)) and 
other <>keanu  return other.name, count(other) as count order by count DESC;

match (keanu:Person {name: "Keanu Reeves"})-[:ACTED_IN]->(movie)
<-[:ACTED_IN]-(actor), (actor)-[:ACTED_IN]->()<-[:ACTED_IN]-(other)  
where NOT ((keanu)-[:ACTED_IN]->(movie)<-[:ACTED_IN]-(other)) and 
other <>keanu  return other.name, count(other) as count order by count DESC;

The only difference is that I specified 'movie' variable. I just want to look up the actors that have not played with Keanu, but have played with his co-stars most frequently. The results are the same, except when I specify a 'movie' variable a new actor is added to the top of the results (having most frequently acted with Keanu's co-stars). That actor does not show up at all in the first query results, but only in the second and tops the results.

The first variant contains

where NOT ((keanu)-[:ACTED_IN]->()<-[:ACTED_IN]-(other)) 

The second one

where NOT ((keanu)-[:ACTED_IN]->(movie)<-[:ACTED_IN]-(other))

So the first one filters out all paths where keanu acts together with other in any movie. The second one filters out all paths where keanu acts together with other in this movie.

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