Hi there,
I'm studying the query below, and wondering why the result co-actors didn't show actor Meg Ryan herself?
Suppose after we get all movie list in variable m, then the second MATCH pattern should list out all the actors who ACTED_IN movies list?
Many thanks!
MATCH (meg:Person)-[:ACTED_IN]->(m:Movie)<-[:DIRECTED]-(d:Person),
(other:Person)-[:ACTED_IN]->(m)
WHERE meg.name = 'Meg Ryan'
RETURN m.title as movie, d.name AS director , other.name AS `co-actors`
Comma-separated MATCH are actually considered part of the same pattern.
The two results below are different.
MATCH (meg:Person)-[:ACTED_IN]->(m:Movie),
(other:Person)-[:ACTED_IN]->(m)
WHERE meg.name = 'Meg Ryan'
RETURN m.title as movie, other.name AS `co-actors`
MATCH (meg:Person)-[:ACTED_IN]->(m:Movie)
MATCH (other:Person)-[:ACTED_IN]->(m)
WHERE meg.name = 'Meg Ryan'
RETURN m.title as movie, other.name AS `co-actors`
I had exactly the same question! After koji’s answer, I still do not understand how things work.
In the first query, we are looking for the Person-nodes that have acted in a movie. We are not interested in all those nodes, but just the node with name Meg Ryan. In the second part of the query, for each movie, we look for all Person-nodes that have acted in that particular movie. Of course, Meg Ryan is one of those. Now, is the query processor that smart that it does not include the Person-node with name Meg Ryan in the result set named other? If so, what is the logic behind this? ‘The Person-node with name Meg Ryan was already selected, so I don’t have to select that Person-node again.’ Is that how it works?
Koji, could you please elaborate some more on the difference between the two queries?
(Perhaps my confusion stems from my history using SQL for relational databases!)
MATCH (meg:Person)-[:ACTED_IN]->(m:Movie),
(tom:Person)-[:ACTED_IN]->(m),
(other:Person)-[:ACTED_IN]->(m)
WHERE meg.name = 'Meg Ryan'
AND tom.name = 'Tom Cruise'
RETURN m.title AS movie, other.name AS `co-actors`
Cypher uses something called relationship isomorphism here, which means that per path, a relationship can only be traversed once.
In the first query, there is only one MATCH, and even though the two parts of the pattern are separated, they are still part of the same path (and multiple paths can be returned).
MATCH (meg:Person)-[:ACTED_IN]->(m:Movie),
(other:Person)-[:ACTED_IN]->(m)
WHERE meg.name = 'Meg Ryan'
RETURN m.title as movie, other.name AS `co-actors`
To fulfill the first pattern of the MATCH, there is only a single :ACTED_IN relationship between Meg Ryan and each movie she's acted in. Once that relationship is traversed, then it cannot be reused when finding the other persons who acted in that movie, which is why Meg doesn't show up in the subsequent results.
However in the second query, the patterns are now broken up into two MATCH clauses, and each one will produce a separate path:
MATCH (meg:Person)-[:ACTED_IN]->(m:Movie)
MATCH (other:Person)-[:ACTED_IN]->(m)
WHERE meg.name = 'Meg Ryan'
RETURN m.title as movie, other.name AS `co-actors`
Because of this, the relationships traversed when matching the path from the first MATCH can be freely used to fulfill the pattern in the second MATCH, so Meg appears as one of the results.