3
votes

In the Movie Graph which comes with Neo4j as Sample graph, I want to query : Get the movie which has been directed and written by the same person who directed The Matrix movie. Here is the query which i am using

Match(m:Movie{title:'The Matrix'})<-[r:DIRECTED]-(p:Person)-[r2]->(n:Movie)
where type(r2)='DIRECTED' and type(r2)='WROTE' 
return p,n

This query is not working but if i will put OR clause instead of AND clause. It will work. Using OR clause giving me the list of movie which is written or directed by the same director who directed The Matrix movie. Please let me know why AND clause is not working with type(r).

2
Your use of AND is confusing, because you have assigned an alias to a specific relationship and your AND refers to the same alias. You need a join, which the equivalent of doing: MATCH (m:Movie{title:'The Matrix'})<-[:DIRECTED]-(p:Person)-[:DIRECTED]->(n:Movie) OPTIONAL MATCH (p:Person)-[:WROTE]->(n:Movie) RETURN p,n I think that gets you slightly closer to what you need - Oliver Frost
I am using alias so that i could check for multiple relationship in where clause using AND operator. Let me know if that is not right way of implementation. - RCS

2 Answers

2
votes

If you have both relationships DIRECTED and WROTE between person and movie you will have two result lines, not one. Like

a |type(r) |b
a |DIRECTED|b
a |WROTE   |b

So when you set or, you get both lines. But when you say and you actually get nothing becaues Directed <>(Directed and Wrote) at the same time If you mean that there should be both DIRECTED and WROTE relationships between person and Movie, query should be like

Match (m:Movie{title:'The Matrix'})<-[r:DIRECTED]-(p:Person)-[r2:DIRECTED]->(n:Movie), 
      (p)-[r3:WROTE]->(n) 
return p,n
1
votes

This is probably the most straightforward query for finding all movies that were both written and directed by the director of "The Matrix":

MATCH (:Movie{title:'The Matrix'})<-[:DIRECTED]-(p:Person)
MATCH (n:Movie)<-[:WROTE]-(p)-[:DIRECTED]->(n)
RETURN p,n;