Dennis
Dennis

Reputation: 181

Cypher query with variable length path

My Cypher query looks like this:

match (n1:N1)-[r1:R1]->()<-[r2:R2*0..]-(n3)<-[r3:R3]-(n4) return *

Because of the 0 in the variable length relationship (<-[r2:R2*0..]-), I was expecting this query to be the same as the following (n2 added):

match (n1:N1)-[r1:R1]->(n2)<-[r2:R2*0..]-(n3)<-[r3:R3]-(n4) return *

but they return quite different results. Can somebody help me understand why this is the case? Is it just that the 0 is not allowed? Thanks.

Upvotes: 1

Views: 1179

Answers (1)

InverseFalcon
InverseFalcon

Reputation: 30397

Your second query has a variable not present in the first query, so of course your results will be different, there will be an extra column.

If we take the relevant fragment of your first query:

(n1:N1)-[r1:R1]->()<-[r2:R2*0..]-(n3)

This means, from the unbound node in the pattern '()', we will traverse 0 or more relationships of type :R2 and the resulting nodes will be bound to the n3 variable. So that unbound node will always be present within the n3 column, whether or not there are any :R2 relationships present to traverse.

Your second query will produce very similar results, each of the columns should match upon the same data, with the exception that there is an extra column, n2, which will always match upon that previously unbound node. That same node will still appear in the n3 column as well.

If this doesn't solve the issue, you may want to better explain what in the results doesn't make sense to you, and what you believe the 0 is doing within your query (0 is definitely allowed in variable length relationships).

Upvotes: 1

Related Questions