neo4j:到其他路径上所有特定节点的现有路径

neo4j: existing paths to all specific nodes on other path

我们有一棵节点树。一些节点被标记为红色。我们还有一个可能嵌套在组(黄色)中的用户 DAG(绿色)。用户和组可以看到红色节点(与非红色节点没有 'see' 关系)

是否可以编写一个密码查询,针对给定的用户 U 和节点 N 检查(或 return N)U(直接或间接通过组)是否可以看到从根路径上的所有红色节点(id = 0) 到 N?

我定义了结构,因此可以根据需要进行任何更改。

示例:

我的尝试:

我可以轻松select从根节点到N的所有读取节点。我可以轻松select路径从U到路径上的红色节点。但我不知道如何定义 'all' 限制。可能吗?

Link 到 Neo4j 控制台:http://console.neo4j.org/?id=bigdba

图表来源:

create (n1:node), (n2:node), (n3:node:red {name:'red'}),
(n1)-[:contains]->(n2), (n2) - [:contains] -> (n3),
(n3) - [:contains] -> (n4:node),
(n2) - [:contains] -> (n5:node),
(n5) - [:contains] -> (n6:node),
(n5) - [:contains] -> (n7:node:red {name:'red'}),

(u1:user) - [:inside] -> (g1:group),
(u1) - [:inside] -> (g2:group),
(u2:user) - [:inside] -> (g1),
(g1) - [:inside] -> (g3:group),

(g3) - [:see] -> (n3),

(u3:user) - [:see] -> (n7)

这是一个使用 filter and all 函数的解决方案:

// match the path between root and n
MATCH p = (r:node)-[*]->(n:node) WHERE id(r) = 0 AND id(n) = 3
// match user u
MATCH (u) WHERE id(u) = 7
// filter all red nodes to the r list
WITH u, p, filter(node in nodes(p) where node:red) as reds
// return true if u can reach all red node
RETURN ALL(red in reds where (u)-[:inside*0..]->(:group)-[:see]->(red))