简单的递归 CYPHER 查询 [英] Simple recursive CYPHER query

查看:19
本文介绍了简单的递归 CYPHER 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常简单的问题,但阅读

那么这里的 *1..2 是什么意思:

* 表示它是一个递归.

1 表示不包括约翰本身,即是起始节点.如果您在此处输入 0,则它还将包含 John 节点 本身.

.. 表示从这个节点到 ...

2 表示递归级别.在这里,您说要停在第 2 级.即不要超越史蒂夫.如果你什么都不放,它会一直运行,直到找不到有"朋友关系的节点

这些类型的查询匹配的

文档此处 和类似的答案 这里.

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:

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).

解决方案

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.

这篇关于简单的递归 CYPHER 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆