Getting paths of any length or long paths does not work

To clarify, this isn't a loop problem. It's an issue of there being a high (limited, but high...millions or billions or higher) number of possible distinct paths when you don't add any restrictions on the types of relationships, the direction, or the upper bounds of the var-length pattern.

Cypher is interested in finding all possible distinct paths that match a pattern. There's nothing that prevents it from revisiting nodes you've visited previously, expanding through them from the same node (but using different relationships) or expanding through previously visited nodes when expanding other paths. The only restriction is that, per path, it cannot traverse the same relationship twice.

If you're interested in getting all distinct nodes, and cutting off traversal and path evaluation if you revisit a node you've visited before, then you may want to leverage APOC Procedures, notably the path expander procs.

In this case:

match (d:Doc{name: 'doc name'}) 
CALL apoc.path.subgraphNodes(d, {}) YIELD node as n
return d, n

subgraphNodes() and some of the other procs use a different kind of uniqueness behavior during traversal, such that a node can only be visited once total, so any other paths that encounter an already visited node are pruned and won't be further evaluated or expanded.

1 Like