Terence Chow
Terence Chow

Reputation: 11153

neo4j pass parameter to variable length relationship

How do I use parameters with variable length relationships?

MATCH path=(:Person {id: {id}})=[:HAS_FRIEND*0..{num_friends}]->(:Person)

I'm trying to create a generic query so that I can pass a value 'num_friends' into the cypher query for various levels of relationships that I need.

I get an error so I'm wondering how something like this would be done?

Upvotes: 0

Views: 510

Answers (1)

stdob--
stdob--

Reputation: 29172

Parameters can not be used as hops count.

But you can use path expander from apoc:

match (P:Person {id: {id}}) with P
call apoc.path.expand( P, 'HAS_FRIEND>', 'Person', 0, {num_friends}) yield path
return path

Adapted for comment:

match (P:Person {id: {id}}) with P
call apoc.path.expand( P, 'HAS_FRIEND>', 'Person', 0, {num_friends}) yield path
with path, last(nodes(path)) as lst where not (lst)-[:HAS_FRIEND]->(:Person)
return path

Upvotes: 2

Related Questions