dinesh707
dinesh707

Reputation: 12592

How to generate relationships using property information [Node4j]

I have imported a CSV where each Node contains 3 columns. id, parent_id, and title. This is a simple tree structure i had in mysql. Now i need to create the relationships between those nodes considering the parent_id data. So each node to node will have 2 relationships as parent and child. Im really new to node4j and suggestions ?

i tried following, but no luck

MATCH (b:Branch {id}), (bb:Branch {parent_id})
CREATE (b)-[:PARENT]->(bb)

Upvotes: 2

Views: 296

Answers (1)

Dave Bennett
Dave Bennett

Reputation: 11216

It seems as though your cypher is very close. The first thing you are going to want to do is create an index on the id and parent_id properties for the label Branch.

CREATE INDEX ON :Branch(id)

CREATE INDEX ON :Branch(parent_id)

Once you have indexes created you want to match all of the nodes with the label Branch (I would limit this with a specific value to start to make sure you create exactly what you want) and for each find the corresponding parent by matching on your indexed attributes.

MATCH (b:Branch), (bb:Branch)
WHERE b.id = ???
  AND b.parent_id = bb.id
CREATE (b)-[:PARENT]->(bb)

Once you have proved this out on one branch and you get the results you expect I would run it for more branches at once. You could still choose to do it in batches depending on the number of branches in your graph.

After you have created all of the :PARENT relationships you could optionally remove all of the parent_id properties.

MATCH (b:Branch)-[:PARENT]->(:Branch)
WHERE exists(b.parent_id)
REMOVE b.parent_id

Upvotes: 2

Related Questions