16

This is an extremely simple question, but reading through the docs for the first time, I can't figure out how to construct this query. Let's say I have a graph that looks like:

enter image description here

and additionally each person has an age associated with them. What CYPHER query will get me a list of John's age and all the ages of the entire friend tree of John?

What I've tried so far:

MATCH (start)-[:friend]>(others)
 WHERE start.name="John"
 RETURN start.age, others.age

This has several problems,

  1. It only goes one one one friend deep and I'd like to go to all friends of John.

  2. It doesn't return a list, but a series of (john.age, other.age).

Hooked
  • 84,485
  • 43
  • 192
  • 261

1 Answers1

39

So what you need is not only friend of john, but also friends of friends. So you need to tell neo4j to recursively go deep in the graph.

This query goes 2 level deep.

MATCH (john:Person { name:"John" })-[:friend *1..2]->(friend: Person)
RETURN friend.name, friend.age;

To go n nevel deep, don't put anything i.e. *1..

Oh and I also found this nice example in neo4j

So what does *1..2 here means:

* to denote that its a recursion.

1 to denote that do not include john itself i.e. is the start node. If you put 0 here, it will also include the John node itself.

.. to denote that go from this node till ...

2 denotes the level of recursion. Here you say to stop at level 2. i.e. dont go beyond steve. If you put nothing there, it will keep on going until it cannot find a node that "has" a friend relationship

Documentation for these types of query match is here and a similar answer here.

Loxley
  • 1,781
  • 17
  • 20
Rash
  • 7,677
  • 1
  • 53
  • 74
  • yes, that will take you all the way deep. So if steve had a friend x, and x had a friend y, all will come in. – Rash Jun 26 '15 at 18:51
  • Thanks for the example @Rash this will go a long way! I just figured out if I start at `0` instead of `1` I get the original node too. – Hooked Jun 26 '15 at 18:57
  • @Hooked Thanks. but damn the documentation. I am trying to find the definitions but I cannot locate them. All i wrote is from what I remember. – Rash Jun 26 '15 at 18:59
  • @Hooked Hi. I have updated the answer with the documentation that I can find. – Rash Jun 26 '15 at 19:08
  • Thanks again for the links! – Hooked Jun 26 '15 at 19:17
  • 2
    There is a nice explanation of recursion lengths here (including pictures): https://graphaware.com/graphaware/2015/05/19/neo4j-cypher-variable-length-relationships-by-example.html – Snorfalorpagus Oct 17 '18 at 14:43
  • How do you also return the "parent" columns, i.e. "who" has friend who. For instance last row would have column values: Sara, Maria, Maria.age – Brian Wiley Oct 24 '20 at 09:12
  • Ok you would probably do something like for the Persons example in `:play cypher` this query `MATCH p = (a:Person { name:"Emil" })-[k:KNOWS *1..2]->(kn: Person) RETURN nodes(p)[length(p)-1].name, length(p), kn.name, kn.from;` – Brian Wiley Oct 24 '20 at 09:34