首页 > 解决方案 > 在 neo4j 中使用 cypher 收集不同路径长度的节点

问题描述

我正在尝试收集节点的不同路径长度以及基于路径长度分配变量的目标。不在路径中的节点 = 分离,路径长度 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})

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

标签: neo4jcypher

解决方案


您的查询有些奇怪:

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

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


推荐阅读