'optional match" not returning null values within Neo4j

Viewed 73

I am writing a query to find out all the directors who have also acted in the movie.

I have written the following query to achieve that result:

optional match (director:Person)-[:DIRECTED]->(movie:Movie)<-[:ACTED_IN]-(director)
return director.name, movie.title

The query is not returning the null values for the director name. My understanding is that "optional match" act as an "outer join" and it should return null values as well.

1 Answers

First of all, you are doing a query for entire path from person to movie to actors, thus optional match will return null if there is no such data exists. Lets say:

optional match (director:Person)-[:DIRECTS]->(movie:Movie)<-[:ACTS]-(director)
return director.name, movie.title

╒═══════════════╤═════════════╕
│"director.name"│"movie.title"│
╞═══════════════╪═════════════╡
│null           │null         │
└───────────────┴─────────────┘

When the documentation says optional match is an "outer join", it means that you should be doing this:

   match (director:Person)-[:DIRECTED]->(movie:Movie)
   optional match (movie)<-[:ACTED_IN]-(director)
   return director.name, movie.title

Ref: https://neo4j.com/docs/cypher-manual/current/clauses/optional-match/#optional-relationships

Related