【问题标题】:neo4j: existing paths to all specific nodes on other pathneo4j:其他路径上所有特定节点的现有路径
【发布时间】:2018-01-25 09:51:28
【问题描述】:

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

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

我定义了结构,以便在需要时可以进行任何更改。

例子:

  • 条件不满足 U=12 和(N = 3 或 N = 红色高于 3)
  • 满足 U = 7 和 N = 3 的条件

我的尝试:

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

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)

【问题讨论】:

    标签: graph neo4j cypher graph-databases


    【解决方案1】:

    这是使用filterall 函数的解决方案:

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

    【讨论】:

    • 它将红色节点从u 带到n,而不是从rootn,但这对我很有帮助,谢谢!
    • @piotrek 欢迎您!但这对我来说并不完全清楚。您的示例数据没有“根”节点...我是不是忘记了什么?
    • 如问题所述,root 是 id 为 0 的节点
    • @piotrek 哦,对不起,我的错!所以我认为这个查询应该很好用:// 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)-[*]->(red))
    • 经过一些微调后,它确实有效。我需要将where (u)-[*]->(red) 更改为where (u)-[:inside*0..]->(:group)-[:see]->(red) 以使see 关系不具有传递性。请更新答案,以便我接受它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-23
    相关资源
    最近更新 更多