Variable Path Relationships with Start Nodes


(Rcfro2) #1

These 2 queries seem to be somewhat similar thought they return very different results.

match (fof:Person)-[:HAS_CONTACT*2]-(p:Person{name:"Tom Hanks"})-[:ACTED_IN]->(m:Movie)
where fof<>p
with collect(distinct fof.name) as f
return f, size(f)

vs

match (p:Person{name:"Tom Hanks"})-[:HAS_CONTACT*2]-(fof:Person)-[:ACTED_IN]->(m:Movie)
where fof <> p
with collect(distinct fof.name) as f
return f, size(f)

The :HAS_CONTACT relationship is an arbitrary relationship. See dataset here: https://github.com/Manish-Giri/Neo4J-GraphDB-Foundations/blob/master/Data/create-course-data.cypher


(Ryan Boyd) #2

The first query is asserting that Tom Hanks had to act in the movie, and gets his contacts (up to 2 degrees, whether they're actors or not).

The second query is asserting that any contact (up to 2 degrees) of Tom Hanks could be an actor. Many of his contacts are not actors:

Those seem quite different to me-- unless I'm missing something?

If you add the restriction that the fof int he first query has to be an actor, you get the same count of results.

AND size( (fof)-[:ACTED_IN]->() ) > 0

Hope this helps!