cyphercypher-shell

neo4j Parent child relationship of n-levels


I have Parent child relationships of say 10-11 levels like shown below and I need to create a relationship between them as parent child

data format

id,parentid

1,0

2,1

3,2

4,3

5,4

6,5

what I tried so far?

I have used the below code to relate them as parent child

LOAD CSV WITH HEADERS FROM 'file:///parent_child.csv' AS line

MERGE (thisThingHere:employee {id: line.id })
MERGE (parent:Element { id: line.parentid })
MERGE (thisThingHere)-[:PARENT]->(parent)

the result of above code is creating parent child relations but they are relating up to just one level, like shown in the below image (available as a link), I need a way to relate them and display it like a tree eg. 3 is a parent of 4 and 2 is a grand parent of 4 and 1 is great grand parent of 1, can anyone please help me on how can I achieve it?

result of my query above


Solution

  • You need to use the same node label for both parent and child node to construct a tree

    LOAD CSV WITH HEADERS FROM 'file:///parent_child.csv' 
    AS line
    
    MERGE (thisThingHere:Element {id: line.id })
    MERGE (parent:Element { id: line.parentid })
    MERGE (thisThingHere)-[:PARENT]->(parent)