Neo4J Cypher: Why is assigning nodes to variables required in the shortestPath function

Viewed 102

When I try to execute a shortestPath() function between a Person node and a Movie node, like -

MATCH p=shortestPath((:Person)-[*1..4]->(:Movie))
RETURN length(p)

This fails with the error -

Neo.ClientError.Statement.SyntaxError
shortestPath(...) requires named nodes (line 1, column 9 (offset: 8))
"MATCH p=shortestPath((:Person)-[*1..4]->(:Movie))"
         ^

To fix this, I must assign the node labels to variables, like so -

MATCH pt=shortestPath((p:Person)-[*1..4]->(m:Movie))
RETURN length(pt)

This produces the correct return -

length(pt)
1
1
1
1
1
1
1
1
2
2
3

and so on.

Why is this required? I'm not using the variables p or m anywhere in my RETURN clause.

I couldn't find an explanation in the shortestPath() documentation.

1 Answers

I have followed your question to the team during the open cypher meeting in Monday, on this is the answer :

There is no need for those variables any more. there might have been in the past, and the check remains for legacy reasons. it's a fair feature request.

Cheers

Related