0
votes

I have nodes where each has a property value that represents a hierarchy. The number of levels in the hierarchy can vary and the existing node always appears at the end of string. The separator is '/'.

An example is:

Property1: '/Level3/Level2/Level1' Property2: 'Level1'

Therefore my question is can I use somekind of string function to create new nodes (if they dont exist) for level2, level3, level4 etc and then merge relationships between level 1 - level 2, level 2 - level 3 etc.?

1

1 Answers

3
votes

This is a simple example to help you :

WITH split("Level2/Level3/Level4/Level5", "/") AS hierarchy 
UNWIND range(1, size(hierarchy)-1) AS i 
MERGE (l1:Level {name: hierarchy[i-1]}) 
MERGE (l2:Level {name: hierarchy[i]}) 
MERGE (l1)-[:PARENT]->(l2)

Results in

enter image description here