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

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

问题描述

这是一个非常简单的问题,但请通过文档阅读 第一次,我不知道如何构造此查询.假设我有一个看起来像这样的图:

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:

,此外,每个人都有与其相关的年龄. 哪些CYPHER查询会给我列出约翰的年龄以及约翰整个朋友树的所有年龄?

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

这有几个问题,

  1. 只有一个一对一的朋友深入,我想和约翰的所有朋友一起去.

它不返回列表,而是返回一系列(john.age, other.age).

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

推荐答案

因此,您需要的不仅是john的朋友,而且是朋友的朋友.因此,您需要告诉neo4j递归地深入图中.

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.

此查询深入2级.

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

要深入n,请不要放置任何东西,即*1..

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

哦,我还在neo4j中找到了这个很好的例子

Oh and I also found this nice example in neo4j

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

So what does *1..2 here means:

*表示它是递归的.

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

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表示递归级别.在这里,您说要停在2级.即不要超越史蒂夫.如果您什么都没放,它将一直继续下去,直到找不到有"朋友关系的节点为止

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

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

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

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