简体   繁体   中英

How to use WHERE Clause in Cypher/Neo4j

I have built this database on Neo4j

在此处输入图片说明

I'm trying to find the actors that played in movies in which Keanu Reeves also played. My code is:

MATCH (a:Actor)-[r:ACTS_IN]->(m:Movie)
WHERE (m:Movie)<-[r:ACTS_IN]-(:Actor { name:"Keanu Reeves" })
RETURN a.name

But it doesn't work. I'm new to this language and I'm not even sure if I should use WHERE or WITH or something else.

Your query uses the same identifier, r , in the MATCH and WHERE clauses. That means the 2 ACTS_IN relationships are supposed to be identical. Therefore, a will always be the node for Keanu Reeves .

This form of your query should return all relevant actors (including Keanu), but would probably not be very performant:

MATCH (a:Actor)-[r:ACTS_IN]->(m:Movie)
WHERE (m)<-[:ACTS_IN]-(:Actor { name:"Keanu Reeves" })
RETURN DISTINCT a.name;

This query should be faster (and eliminate Keanu from the result), especially if you first create an index on :Actor(name) :

MATCH (a:Actor)-[:ACTS_IN]->(:Movie)<-[:ACTS_IN]-(:Actor { name:"Keanu Reeves" })
RETURN DISTINCT a.name;

First: find the list of movies that Keanu acted in.

Second: WITH Keanu and the each of movies find the other actors in those movies that aren't Keanu.

Third: return the distinct list of Keanu's colleagues with which he acted across the list of movies

match (k:Actor {name: 'Keanu Reeves'})-[r:ACTS_IN]->(m:Movie)
with k,m
match (m)<-[:ACTS_IN]-(colleague:Actor)
where colleague <>  k
return distinct(colleague)

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