1

I was curious as to how neo4j interprets the following query and what is actually returned.

MATCH path=(p1:Student)-[f:Friends]->(p2:Student)-[f2:Friends]->(p3:Student) 
RETURN p1.studentID, p2.StudentId, p3.StudentId

I see this as returning all nodes in a chain that link to another,to another and to another. However what if the data contains chains with 4 nodes. Does this query: 1) return nothing 2) return the first 3 nodes of the chain or 3) return the last 3 nodes in the chain of 4?

example drawing of 2) and 3)

Thanks in advance, only just started using cypher and couldn't find anything about this.

Dave Bennett
  • 10,996
  • 3
  • 30
  • 41
Stig
  • 353
  • 1
  • 3
  • 12

1 Answers1

2

The answer is ... both 2) and 3) :

Try this :

CREATE CONSTRAINT ON (s:Student) ASSERT s.StudentID IS UNIQUE;

CREATE (s1:Student {StudentID: "1"}),(s2:Student {StudentID: "2"}),(s3:Student {StudentID: "3"}),(s4:Student {StudentID: "4"}),(s1) -[:FRIENDS]-> (s2) -[:FRIENDS]-> (s3) -[:FRIENDS]-> (s4);

MATCH (sm1:Student) -[:FRIENDS]-> (sm2) -[:FRIENDS]-> (sm3) 
RETURN sm1.StudentID, sm2.StudentID, sm3.StudentID;

Will return :

1, 2, 3

2, 3, 4

Or in other words, the match does not care where in the graph the condition is satisfied.

Hope this helps, Tom

Dave Bennett
  • 10,996
  • 3
  • 30
  • 41
Tom Geudens
  • 2,638
  • 9
  • 15