How to generate relationships using property information [Node4j]

不问归期 提交于 2019-12-11 03:56:52

问题


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)

回答1:


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


来源:https://stackoverflow.com/questions/28409743/how-to-generate-relationships-using-property-information-node4j

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!