Simple recursive CYPHER query

前端 未结 1 1596
星月不相逢
星月不相逢 2021-01-02 01:36

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

相关标签:
1条回答
  • 2021-01-02 02:18

    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.

    0 讨论(0)
提交回复
热议问题