在 Neo4j 中到达第一个必需节点时停止遍历



抱歉,如果这个问题看起来很模糊:)我正在寻找的查询有点具体。考虑图形中的以下分支:

( a:Detector {prop_x:False})-->( b:Category {prop_x:False})-->( c:Category {prop_x:True})-->(

d:Category {prop_x:True})-->(...) 等等

现在我想获取a的所有父节点,直到我到达节点的属性prop_x为 True 的节点,然后停止。 即我想要路径:

( a:Detector {prop_x:False})-->( b:Category {prop_x:False})-

->( c:Category {prop_x:True})

我尝试了以下查询:

match path=(child:Detector)-[*]->(parent:Category {prop_x:True}) return path

但我得到的路径还包括节点(d:Category),因为它也有prop_x True。

我希望图形中的所有此类路径Detector 个节点开始,直到第一个"父"节点Category节点prop_x True

您那里的表达式将匹配所有路径,这些路径以具有标签的节点开头,Label,以带有标签的节点结束 Label 并且prop_x true。 我假设你知道你要从哪里开始,并从name A的节点开始。 然后匹配从那里结束的所有路径,并以true prop_x结束。 这也可能包括它true - false - true路径。 我按路径长度对生成的路径进行升序排序,并仅保留顶部匹配项。

match path=(child:Node {name: 'A'})-[*]->(parent:Node {prop_x:True})
return length(path), nodes(path)
order by length(path)
limit 1

以下新增内容

更新每个更新的问题,"如何从所有Detector Nodes中找到所有这些路径?

我想

了一会儿,这就是我想出的......

// first match all of your detector nodes
match (d:Detector)
with d
// then for each detector node match the paths that end with True
match path=d-[*]->(parent:Category {prop_x:True})
// for each detector collect the length and matching nodes
with d, [ length(path), nodes(path) ] as path_match
// order by the detector name and path length so they are grouped and sorted
order by d.name, length(path)
// then collect all of the length, path collections so there is one
// row per detector
with d, collect(path_match) as path_matches
// then return the first Detector and the first (i.e. shortest) collection in the collection of paths
return d.name, path_matches[0]

最新更新