递归查找Neo4j中给定节点的所有父节点

huangapple go评论52阅读模式
英文:

Recursively find all parent nodes for a given node in neo4j

问题

我想编写一个Cypher查询,根据给定的节点X,找到该节点的所有父节点,直到找到具有类型属性为ROOT的根节点。

示例1:查找标签为TYPE2:X3的节点的所有父节点
从图中可以看出,TYPE2:X3有一个父节点TYPE2:X1。现在,TYPE2:X1有两个父节点TYPE1:T1和RootNode。递归查找TYPE1:T1的父节点,即RootNode。因此,答案将是TYPE1:T1和TYPE2:X1。

示例2:查找标签为TYPE2:X4的节点的所有父节点
从图中可以看出,TYPE2:X4有4个父节点TYPE1:T1、TYPE2:X1、TYPE2:X2和TYPE1:T2,它们的父节点都是RootNode,因此答案将是这4个节点。

请注意,我的图可以有多达10个级别的类似父节点。

英文:

I want to write a cypher query where given a node X, it gives all the parent nodes for that given node until I find the root node which has the type attribute as ROOT.

As an example, I have attached below image where my RootNode is the main parent node and it has attribute {type: "ROOT"}.

递归查找Neo4j中给定节点的所有父节点
> Example1: Find all parent nodes for a node with label TYPE2:X3
From the graph we can see, TYPE2:X3 has one parent nodes TYPE2:X1. Now TYPE2:X1 has two parents TYPE1:T1 and RootNode. Recursively, finding parent of TYPE1:T1 which is RootNode. Hence, the answer will be TYPE1:T1 and TYPE2:X1

> Example2: Find all parent nodes for a node with label TYPE2:X4
From the graph we can see, TYPE2:X4 has 4 parent nodes
TYPE1:T1, TYPE2:X1, TYPE2:X2, TYPE1:T2 who all have parent as RootNode so the answer will be these 4 nodes.

Please note that my graph can have upto 10 level of parent nodes like this.

答案1

得分: 0

以下是翻译好的内容:

最简单的做法就是以可变路径长度遍历图形:

match path = (s)-[*..9]->()-->(e)
where s:X4 and s:TYPE2 and e:ROOT
with [n in nodes(path) where n <> e | n] as parentnodes
return parentnodes

可变路径长度可能会导致查询扩大,尤其是如果有超级节点的情况。如果你的图结构像你的图表中那样相对平衡,这可能是可以的。

更新:
这将使您不需要知道根节点的标签:

match path = (s)-[*..9]->()-->(e)
where s:X4 and S:TYPE2 and not (e)->()
with [n in nodes(path) where n <> e | n] as parentnodes
return parentnodes
英文:

The simplest thing to do is just traverse the graph with variable path length:

match path = (s)-[*..9]-&gt;()--&gt;(e)
where s:X4 and s:TYPE2 and e:ROOT
with [n in nodes(path) where n &lt;&gt; e | n] as parentnodes
return parentnodes

Variable path length can make the query explode, especially if you have supernodes. If you have a fairly balanced tree structure like in your diagram, this may be okay.

UPDATE:
This will make it so you don't need to know the label on the root node:

match path = (s)-[*..9]-&gt;()--&gt;(e)
where s:X4 and S:TYPE2 and not (e)-&gt;()
with [n in nodes(path) where n &lt;&gt; e | n] as parentnodes
return parentnodes

答案2

得分: 0

这个查询将返回从给定的 n 节点开始,通过跟随所有出站关系的方式获得的所有不同的 ancestor 节点,不包括没有出站关系的节点。

此查询还对可变长度模式设置了上限,以避免运行时间过长或内存不足。

x[1..-1] 语法指定了一个 x 的子列表,包括第二个元素到倒数第二个元素。(文档有点误导,因为它集中在 range() 函数上,而指定子列表与是否使用 range() 函数无关。这两个概念应该分开介绍。)

英文:

[UPDATED]

This query will return all distinct ancestor nodes by following all outbound relationships starting at a given n node, excluding any node that has no outbound relationships.

... // (prior Cypher providing n)

MATCH p = (n)-[*..8]-&gt;(root)
WHERE NOT EXISTS((root)--&gt;())
UNWIND NODES(p)[1..-1] AS ancestor
RETURN DISTINCT ancestor

This query also puts an upper bound on the variable length pattern to avoid taking forever or running out of memory.

The x[1..-1] syntax specifies a sublist of x consisting of its second element up to and including its next-to-last element. (The documentation is a bit misleading, as it concentrates on the range() function, whereas specifying a sublist does not depend on using the range() function at all. The 2 concepts should have been covered separately.)

huangapple
  • 本文由 发表于 2023年6月8日 05:51:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427321.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定