2
votes

I'm trying out the Cypher examples on the Neo4j website and one of the queries has me a bit puzzled.

There are three movie nodes with respective titles "The Matrix", "The Matrix Reloaded" and "The Matrix Revolutions". The Matrix node has id "603".

There are also three actor nodes with respective names "Keanu Reeves", "Laurence Fishburne" and "Carrie-Anne Moss".

There are ACTS_IN relations between all actors and all movies.

This is a query that's supposed to return all the other movies that the actors in The Matrix acted in:

START matrix=node:node_auto_index(id="603") 
MATCH (matrix)<-[:ACTS_IN]-(actor)-[:ACTS_IN]->(movie) 
RETURN actor.name + ' acts in ' + movie.title;

And this is its (correct) result:

Keanu Reeves acts in The Matrix Reloaded
Keanu Reeves acts in The Matrix Revolutions
Laurence Fishburne acts in The Matrix Reloaded
Laurence Fishburne acts in The Matrix Revolutions
Carrie-Anne Moss acts in The Matrix Reloaded
Carrie-Anne Moss acts in The Matrix Revolutions

Why is The Matrix node itself not included in the results?

1

1 Answers

3
votes

The MATCH clause specifies a path. A path might have loop, but will never contain the same relationship twice. In

MATCH (matrix)<-[:ACTS_IN]-(actor)-[:ACTS_IN]->(movie) 

the two ACTS_IN relationships are always different. Therefore matrix is always not equal to movie since none of the actors has two ACTS_IN relationships to the same movie.

If you add multiple relationships form one actor to the same movie you'll get back matches having movie="The Matrix". A reason for this might be if an actor plays multiple characters.

Update regarding 1st comment below:

In this case the query in natural languages would be: which movies have all the actors of the matrix acted in?

START matrix=node:node_auto_index(id="603") 
MATCH matrix<-[:ACTED_IN]-actor
WITH actor
MATCH actor-[:ACTED_IN]->movie
RETURN distinct movie.title;

The query is build up of two parts chained with the WITH statement:

  1. find actors of the matrix
  2. find all movies for those actors