【问题标题】:collecting nodes of varying path length using cypher in neo4j在 neo4j 中使用 cypher 收集不同路径长度的节点
【发布时间】:2018-09-07 08:57:13
【问题描述】:

我正在尝试收集不同路径长度的节点以及根据路径长度分配变量的目标。不在路径中的节点 = 分离,路径长度 1 = 半,路径长度 > 1 = 梯形。

我有以下密码,但是在返回收集的列表时没有返回任何内容,即使每个部分都可以正常工作。

match (a:Test) where not (a)-[]-() with a, COLLECT(DISTINCT a) as detached  
match (a:Test)-[r]-() with a,detached,count(r) as rels where rels = 1 
match path = (a)-[]->() with detached, COLLECT(DISTINCT NODES(path)) AS semis 
match path = (a)-[:NEIGHBOURING_BUILDING*]-() where length(path) > 1 with detached, semis, COLLECT(DISTINCT NODES(path)) AS terraces 
return detached, semis, terraces

我目前正在使用这个测试网络

create (:Test{id:1}) 
create (:Test{id:2})
create (:Test{id:3})-[:NEIGHBOUR]->(:Test{id:4}) 
create (:Test{id:5})-[:NEIGHBOUR]->(:Test{id:6})<-[:NEIGHBOUR]-(:Test{id:7})
create (:Test{id:8})-[:NEIGHBOUR]->(:Test{id:9})
create (:Test{id:10})-[:NEIGHBOUR]->(:Test{id:11})<-[:NEIGHBOUR]-(:Test{id:12})

如何将每种路径中的节点收集到一个列表中?

【问题讨论】:

    标签: neo4j cypher


    【解决方案1】:

    您的查询有些奇怪:

    match (a:Test) where not (a)-[]-() with a, COLLECT(DISTINCT a) as detached  
    match (a:Test)-[r]-() with a,detached,count(r) as rels where rels = 1 
    

    在第一行,您正在搜索一个没有任何关系的节点 a (not (a)-[]-()),然后在第二行,您想要同一节点的关系:(a:Test)-[r]-()

    所以没有结果是正常的......

    【讨论】:

    • 谢谢,是的,这是个问题。然后导致发现其他问题(例如重复计算节点),但这是一个更正的密码匹配 (n:Test),其中不是 (n)-[]-() 与 collect(distinct n) 作为分离匹配 (x:测试)-[]-(y) 其中 (x)() 而不是 ()-[]-(x)-[]-() 与分离,collect(distinct x) as list1,collect (distinct y) as list2 with detached,list1 + list2 as semis match path = (:Test)-[:NEIGHBOUR*]-() where length(path) > 1 unwind nodes (path) as n with detached, semis, collect(distinct n) as terrades 返回 detached, semis, ladders
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    相关资源
    最近更新 更多