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.

Relationship and Where conditions

rcfro2
Node Clone

MATCH (keanu:Person)-[:ACTED_IN]->()<-[:ACTED_IN]-(c), (c)-[:ACTED_IN]->()<-[:ACTED_IN]-(coc)

Without anything else, couldnt all the above indicated nodes be the same node? In other words, "keanu", "c"and "coc" could all refer to Keanu Reeves, for example. Is this correct?

Also, if we specified the following:

MATCH (keanu:Person)-[:ACTED_IN]->()<-[:ACTED_IN]-(c), (c)-[:ACTED_IN]->()<-[:ACTED_IN]-(coc)
WHERE keanu <> c

All we would be guaranteeing is that keanu and c are not equal, so therefore coc and keanu could be right? On that note, wouldnt the following also have a similar reasoning:

MATCH (keanu:Person)-[:ACTED_IN]->()<-[:ACTED_IN]-(c), (c)-[:ACTED_IN]->()<-[:ACTED_IN]-(coc)
WHERE keanu <> coc

Only keanu and coc are not equal, but keanu and c could be equal.

Couldn't the same be said about this query below, in that coActor could refer to keanu?

MATCH (keanu:Person)-[:ACTED_IN]->(movie:Movie),
 (coActor:Person)-[:ACTED_IN]->(movie)
WHERE keanu.name = 'Keanu Reeves'
RETURN DISTINCT coActor.name;
4 REPLIES 4

It's important to understand one specific aspect of a path in Cypher: the very same relationship will never be used multiple times. Otherwise this would allow for infinite recursion.

That said, in your example c is never keanu - unless there are more than one :ACTED_IN relationships between the same actor and same movie.

How about the coActor?

coactor and keanu can be the same if the movie nodes ( the ones referred to as () in your example are different.
Example:
Keanu coacted with Laurence Fishburne coacting in all 3 movies of the matrix trilogy

So a path would be:
(keanu)-[:ACTED_IN]->(matrix)<-[:ACTED_IN]-(laurence)-[:ACTED_IN]->(matrix revolutions)<-[:ACTED_IN]-(keanu)

q.e.d.

I had several examples because I wanted to understand the logic in different contexts. No reason for the q.e.d. 🙂 thanks.