1
votes

I want to match a path between 2 nodes where there are 0 or more of a certain type of relationship but ending in a specific different relationship, eg it should match:

(a) -> [:GRANT] -> (b)
(a) - [:MEMBER] -> () -> [:GRANT] -> (b)
(a) - [:MEMBER] -> () - [:MEMBER] -> () -> [:GRANT] -> (b)

but not

(a) - [:MEMBER] -> () - [:GRANT] -> () -> [:GRANT] -> (b)

Ive tried:

MATCH (a) - [:MEMBER *] -> () - [:GRANT] -> (b)

but it only works if there is at least one member relationship (eg the () node must exist).

Ive also tried things like this:

MATCH (a) - [:MEMBER *] -- [:GRANT] -> (b)

but they have syntax errors.

Is there a way to optionally match a relationship while ensuring the chain ends in a specific second relationship type?

1

1 Answers

3
votes

You're very close, you need to set the minimum of that variable length relationship to 0 like so:

MATCH (a) - [:MEMBER *0..] -> () - [:GRANT] -> (b)

You have the idea now, but here's the link to the section in the docs just in case.