cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Neo4j Training/ Querying with Cypher in Neo4j 4.x/ Working with Patterns in Queries

I fail to comprehend how Meg Ryan is excluded from the resulting list of the names of other:Person by this example cypher code given us in the subject lesson:

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

How (or why) does the query processor make the determination to exclude Meg Ryan from the expression (other:Person)-[:ACTED_IN]->(m)?

Coming from an SQL background, I fail to see why (other:Person)-[:ACTED_IN]->(m) implicitly excludes (meg:Person {name: 'Meg Ryan}).

I presume it is implicit, but would so love to read an explanation--perhaps a pointer to an clearly-written article explaining more fully the mechanics.

Thank you,
Tim

PS The lesson material immediately following, involving example using Val Kilmer, makes mention of relationships being traversed only once, rather than twice.

Is that where the answer lies? That an additional MATCH is needed if one wanted to see Meg Ryan? Is that the nature of MATCH, that it traverses every relationship only once?

5 REPLIES 5

Hello @palanquinpeacesoluti and welcome to the Community.

When you specify multiple patterns in a single MATCH clause, the query processor never traverses a relationship more than once. In this case the relationship between the Meg Ryan node and the movie nodes has already been traversed. So other will refer to all new nodes that have not been traversed to that movie.

Elaine

I commend your excellent answer. This is good to know with certainty and clarity.

I believe your one brief paragraph would add greatly to the lesson in question, perhaps following the figure depicting the Meg Ryan query's results. And then, one more brief remark contrasting the results, before or after the Val Kilmer example, might more fully illuminate this curricula text: "A best practice is to traverse as few nodes as possible so in this example, using multiple MATCH patterns is best." Spaced repetition is such a great teacher!

Thank you again, Elaine, for answering my question so promptly and succinctly. Blessings!

In my case, I don't fully understand the last paragraph in the Val Kilmer example: " A best practice is to traverse as few nodes as possible so in this example, using multiple MATCH patterns is best.".
Don't multiple patterns specified in one MATCH traverse as few nodes as possible because the query processor never traverses a relationship more than once?

Hello @RaduTheMan,

Welcome to the Community!

What this is saying is that a single MATCH clause with 2 patterns is better than 2 MATCH clauses with patterns.

Elaine

Ok, thank you for your answer and for the greeting . In that phrase I thought that it reffers to multiple match clauses. I made a confusion, my bad.