简体   繁体   中英

collecting nodes of varying path length using cypher in neo4j

I am trying to collect differing path length of nodes together with the goal of assigning a variable based on the path length. Node not in a path = detached, path length of 1 = semi, path length > 1 = terraced.

I have the following cypher but when returning the collected lists nothing is return, even though each part works fine on its on.

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

I am using this test network at the moment

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

How can I collect the nodes in each type of path into a list?

There is something weird on your query :

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 

On the first line your are searching a node a that doesn't have any relationship ( not (a)-[]-() ), and then on second line your want the relationships of this same node : (a:Test)-[r]-() .

So it's normal that you 've got no result ....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM