cancel
Showing results for 
Search instead for 
Did you mean: 

Position of node

thomas_stuempfi
Node Clone

Hi there,

lets assume that there is a tree structure

 

create (n1:Item {MId:"1000",expanded:true})
create (n11:Item {MId:"1100",expanded:false})
create (n12:Item {MId:"1200",expanded:true})
create (n111:Item {MId:"1110",expanded:false})
create (n112:Item {MId:"1120",expanded:false})
create (n113:Item {MId:"1130",expanded:false})
create (n121:Item {MId:"1210",expanded:false})
create (n122:Item {MId:"1220",expanded:false})
create (n1221:Item {MId:"1221",expanded:false})
merge (n1)-[r1:child]->(n11)
merge (n1)-[r2:child]->(n12)
merge (n11)-[r3:child]->(n111)
merge (n11)-[r4:child]->(n112)
merge (n11)-[r5:child]->(n113)
merge (n12)-[r6:child]->(n121)
merge (n12)-[r7:child]->(n122)
merge (n121)-[r8:child]->(n1221)
merge (n122)-[r9:child]->(n1221)

 

now put this in a html grid/treetable like file explorer etc...
Line,MId
1,1000
2,1100
3,1200
4,1210
5,1221
6,1220
7,1221

expanded=false->do not include the "childs" e.g. 1110,1120,1130.
The question is, how to add get the line number? Be aware that it is possible that a node can possibly have multiple parents. Actually it is more a directed acyclic graph than a tree. The traversal shoud "explode" the node each time during recusion.

 

 

1 REPLY 1

glilienfield
Ninja
Ninja

I am not sure exactly what the requirement is, but I will assume you want to know the level of each node in the graph, so you can display then in a 'explorer' type view. The following does that, with ignoring the 'expand' property:

match (n:Item {MId:"1000"})
match p=(n)-[:child*]->(c)
where not exists ((c)-[:child]->())
unwind range(0, length(p)) as index
with {level: index, node: nodes(p)[index].MId} as nodeStats
return  distinct nodeStats
order by nodeStats.level

Screen Shot 2022-08-20 at 12.27.41 PM.png 

Next, I assumed the 'expand' flag meant to not include the node and all its descendant nodes. If true, the following does that:

match (n:Item {MId:"1000"})
match p=(n)-[:child*]->(c)
where all(i in nodes(p) where i.expanded)
unwind range(0, length(p)) as index
with {level: index, node: nodes(p)[index].MId} as nodeStats
return  distinct nodeStats
order by nodeStats.level

Result:

Screen Shot 2022-08-20 at 12.30.39 PM.png

 If these don't meet you needs, can you give an example so I can understand your requirements better? Sorry for my confusion. 

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.