代码之家  ›  专栏  ›  技术社区  ›  SAB

在neo4j中使用cypher收集不同路径长度的节点

  •  0
  • SAB  · 技术社区  · 7 年前

    我试图收集不同路径长度的节点,并根据路径长度分配一个变量。不在路径中的节点=分离,路径长度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})
    

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

    1 回复  |  直到 7 年前
        1
  •  1
  •   logisima    7 年前

    你的问题有点奇怪:

    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]-() .

    所以你没有结果是正常的。。。。