cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to create a PARENT relationship between the child and parent node?

kasipasi
Node Clone

I read this article  about building hierarchical trees in neo4j.

kasipasi_4-1664496106879.png

I have loaded all eclass nodes into the database, its approx 47k nodes, each node is a 8 digits code, the standard have 4 level.

Example
Level 1 - 15000000
Level 2 - 15040000
Level 3 - 15040200
Level 4 - 15040203

So I need to create a PARENT relationship between the Level 1 and Level 2 node(s), and down to Level 4.

15000000 <- PARENT <- 15040000 <- PARENT <- 15040200 <- PARENT <- 15040003

Anyone can point me in right direction how to build such query?

Here is the import script I used to load all the Eclass nodes.

 

 

// IMPORT ECLASS NODES
:auto LOAD CSV WITH HEADERS from 'file:///eclass12-EN.csv' AS row
CALL {
    with row
MERGE (e:Eclass { standard: row.Standard, versionNumber: row.VersionNumber, isoLanguageCode: row.ISOLanguageCode, codedName: row.CodedName, idcc:row.IdCC, preferredName: row.PreferredName, definition: COALESCE(row.Definition, ''), mkKeyword: COALESCE(row.MKKeyword, ''), irdicc: row.IrdiCC })
}
IN TRANSACTIONS OF 1000 ROWS;

 

 

 

kasipasi_0-1664495765883.png

kasipasi_1-1664495791449.png

kasipasi_2-1664495807263.png

kasipasi_3-1664495822047.png

 

 

1 ACCEPTED SOLUTION

Try this.  It is a fairly manual process that assumes four levels. I could probably generalize it to more levels you id had more characters with the same structure.

match(level1:Node) where level1.id ends with '000000'
with level1, substring(level1.id, 0, 2) as level1Prefix
match(level2:Node) where level2.id starts with level1Prefix and level2.id ends with '0000' and level2 <> level1
with level1, level2, substring(level2.id, 0, 4) as level2Prefix
match(level3:Node) where level3.id starts with level2Prefix and level3.id ends with '00' and level3 <> level2
with level1, level2, level3, substring(level3.id, 0, 6) as level3Prefix
match(level4:Node) where level4.id starts with level3Prefix and level4 <> level3
merge(level1)<-[:PARENT]-(level2)<-[:PARENT]-(level3)<-[:PARENT]-(level4)
return level1, level2, level3, level4

 Test data:

create(:Node{id:"15000000"})
create(:Node{id:"15040000"})
create(:Node{id:"15040200"})
create(:Node{id:"15040203"})
create(:Node{id:"12000000"})
create(:Node{id:"12040000"})
create(:Node{id:"12040200"})
create(:Node{id:"12040203"})

Results:

Screen Shot 2022-10-01 at 12.28.22 PM.png

Screen Shot 2022-10-01 at 12.19.46 PM.png

View solution in original post

3 REPLIES 3

What information indicates the parent node of a child node?  Does a child node have its parent node’s ‘id’ as a property? 

kasipasi
Node Clone

Hello @glilienfield,

Thank you for you reply!

What information indicates the parent node of a child node?

The Eclass tree has a logic in the numbers. The first two digits show level 1, the first four digits shows level 2, the first six digits shows level 3 and finally the full 8 digits shows level 4.

15000000 <- PARENT <- 15040000 <- PARENT <- 15040200 <- PARENT <- 15040003

So the code should to something like this:
- iterate over all nodes that have XX000000. 
Lets say first match is 15000000, then iterate over 15XX0000 and find all child nodes, add relationship PARENT to all those with 15000000. After take first child node we found, iterate over all their child nodes and drill down the three. 

Does a child node have its parent node’s ‘id’ as a property?  // no

kasipasi_0-1664531781377.png

 

Try this.  It is a fairly manual process that assumes four levels. I could probably generalize it to more levels you id had more characters with the same structure.

match(level1:Node) where level1.id ends with '000000'
with level1, substring(level1.id, 0, 2) as level1Prefix
match(level2:Node) where level2.id starts with level1Prefix and level2.id ends with '0000' and level2 <> level1
with level1, level2, substring(level2.id, 0, 4) as level2Prefix
match(level3:Node) where level3.id starts with level2Prefix and level3.id ends with '00' and level3 <> level2
with level1, level2, level3, substring(level3.id, 0, 6) as level3Prefix
match(level4:Node) where level4.id starts with level3Prefix and level4 <> level3
merge(level1)<-[:PARENT]-(level2)<-[:PARENT]-(level3)<-[:PARENT]-(level4)
return level1, level2, level3, level4

 Test data:

create(:Node{id:"15000000"})
create(:Node{id:"15040000"})
create(:Node{id:"15040200"})
create(:Node{id:"15040203"})
create(:Node{id:"12000000"})
create(:Node{id:"12040000"})
create(:Node{id:"12040200"})
create(:Node{id:"12040203"})

Results:

Screen Shot 2022-10-01 at 12.28.22 PM.png

Screen Shot 2022-10-01 at 12.19.46 PM.png