Variable Length path - multiple relationships returned


(Rcfro2) #1

Why does this query return more than just the produced relationship ? Also, what does the limit clause in this query represent? I'm not sure exactly what it is limiting to 3.

match p=(k:Person)-[:PRODUCED*2]-(m)
where not (k)-[:PRODUCED]-(m)
return p, length(p)
order by length(p)
limit 3


(Andrew Bowman) #2

This query is matching to a path of length 2 (comprised of 3 nodes and 2 connecting relationships) of a :Person node and two successive :PRODUCED relationships where that person didn't produce the end node of the path.

What's being returned is the path itself along with the length of the path (which will always be 2, so there's really no need to get the length or order the paths since you already know the path length and the results will always be in the desired order), and you're limiting the return to 3 paths.

I'm guessing there's a misunderstanding about what you think the query is doing or is supposed to do? I hope the description provided has helped a bit here.


(Rcfro2) #3

It returns a total of 5 nodes, not 3. And I'm still not sure why it looks for an additional relationship not specified. I have ran this only on the Movie dataset provided by Neo4j, and it returns not just :PRODUCED but also 2 :WROTE relationships.

What you described is what I think I understand...

Alternatively, if I run this -

match p=(k:Person)-[:PRODUCED*2]-(m)
where not (k)-[:PRODUCED]-(m)
return p, length(p)
order by length(p)
limit 1

and limit 1, I get 3 nodes and 2 sets of relationships, :WROTE and :PRODUCED. Why ?


(Andrew Bowman) #4

I think you ought to take a look at the Table result view, I think you're looking at the Graph result view only.

For the nodes displayed, the query is limited to 3 paths, and each of those paths is comprised of 3 nodes. If there are nodes shared among those 3 paths returned, then you may get a number of nodes < 9, so in this case 5.

As for the extra relationships you didn't specify, the browser has a functionality (turned on by default) that queries for all relationships between the nodes returned in a query, which can be useful to see how the result nodes are connected, but aren't needed in this case.

If you open the Browser Settings (gear icon on the lower left), scroll to the bottom and uncheck "Connect result nodes" then you won't see those extra relationships.