Hello fellow Neo4jers/ians!
Going through the lessons under Querying with Cypher and a question -
Related note from the Cypher Manual page -
In the case of multiple
MATCH
/OPTIONAL MATCH
clauses, the predicate inWHERE
is always a part of the patterns in the directly precedingMATCH
/OPTIONAL MATCH
. Both results and performance may be impacted if theWHERE
is put inside the wrongMATCH
clause.
Query -
MATCH (valKilmer:Person)-[:ACTED_IN]->(m:Movie)
MATCH (actor:Person)-[:ACTED_IN]->(m)
WHERE valKilmer.name = 'Val Kilmer'
RETURN m.title as movie , actor.name
How is this block processed? I understand MATCH 1 produces a list of Person nodes who acted in a movie. At this point, the MATCH 2 produces a list of Person nodes who acted in the Movie nodes identified in MATCH 1.
Now when the WHERE is applied to MATCH 2, how is does the condition work on the nodes identified in MATCH 2? I do not see a clear connection between them.
In the OPTIONAL MATCH case, it is a little more intuitive since the WHERE is directly placed after the MATCH statement to which it applies. (at least in the examples I have come across).
Your help is appreciated!