2

I would like to input two specific nodes and return the quantity of relationships that are along the path that connect the specific nodes. (There is only 1 path possible in every case)

In some cases, two specific nodes are related through two relationships like this:

(Tim)-[]-()-[]-(Bill)

Should return 2 (relationships).

In other cases there are more nodes between my specific start and end nodes. Like this:

(Tim)-[]-()-[]-()-[]-()-[]-(Bill)

Should return 4 (relationships).

I have two types of relationships that could exist between nodes, so I need to avoid being specific about the type of relationship if possible.

New to this and performed an extensive search before asking this question as no one seemed to discuss relationships between specific nodes...

Many thanks for your help!

Bruno Peres
  • 15,845
  • 5
  • 53
  • 89
JBoz
  • 43
  • 1
  • 6

1 Answers1

5

This query should work:

match p = (:Person {name:'Tim'})-[*]->(:Person {name:'Bill'})
RETURN length(p)

That is: return the length() of path p.

Bruno Peres
  • 15,845
  • 5
  • 53
  • 89
  • Thanks Bruno, I tried this on an example that should produce 4 relationships and got the result of 1. I'm wondering then, if your solution is counting the path - as there is only one path from the start node to the end node... It may help if I am more specific, please allow me: Each Node is a Train Station, the relationships between stations are the connecting services. MATCH p = (:station {name:'Waterfront'})-[*]-(:station {name:'King George'}) RETURN count(relationships(p)) Between Waterfront and King George, are 3 other Station Nodes. Thus, should have 4 relationships.. – JBoz Jul 18 '17 at 14:25