Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
04-20-2020 05:05 AM
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`
https://neo4j.com/graphacademy/online-training/introduction-to-neo4j/part-5/
04-20-2020 06:31 AM
Hi,
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`
04-20-2020 02:12 PM
Many thanks koji for your explaination!
05-04-2020 01:51 PM
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!)
05-04-2020 04:55 PM
Hi,
‘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?
I think so.
For example, there are six actors in Top Gun.
MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
WHERE m.title = 'Top Gun'
RETURN p.name
p.name
"Kelly McGillis"
"Anthony Edwards"
"Tom Cruise"
"Meg Ryan"
"Tom Skerritt"
"Val Kilmer"
Choose Meg Ryan and Tom Cruise.
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`
If you do that, the result will be 4 people.
movie co-actors
"Top Gun" "Kelly McGillis"
"Top Gun" "Anthony Edwards"
"Top Gun" "Tom Skerritt"
"Top Gun" "Val Kilmer"
Once selected, it will not be selected.
05-10-2020 05:34 PM
The answer for this lies in Cypher's uniqueness behavior when expanding paths.
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.
All the sessions of the conference are now available online