Search code examples
neo4jcypherrelationships

Neo4j: Create slef relation cypher query


I have big number of EmpBase imprted (from csv file from postgreSQL )nodes like:

neo4j-sh (?)$ match (e:EmpBase) return e limit 10;
+-------------------------------------------------------------------------+
| e                                                                       |
+-------------------------------------------------------------------------+
| Node[8992]{neo_eb_id:8993,neo_eb_name:"emp_no_8993",neo_eb_bossID:5503} |
| Node[8993]{neo_eb_id:8994,neo_eb_name:"emp_no_8994",neo_eb_bossID:8131} |
| Node[8994]{neo_eb_id:8995,neo_eb_name:"emp_no_8995",neo_eb_bossID:8624} |

What cypher query can create self relations on every node so that every node with neo_eb_bossid can have the relationship to the adequate node ?

In postgreSQl the data is about 1020MB table. In Neo4j, after import, it is 6.42 GiB as the console says.


Solution

  • In order to create the relationship based on neo_eb_bossID, you can match the nodes and run a foreach loop that will create the relationships to the related node :

    MATCH (e:EmpBase) WITH collect(e) AS empbases
    FOREACH (emp in empbases |
        MERGE (target:EmpBase {neo_eb_id:emp.neo_eb_bossID}
        MERGE (emp)-[:YOUR_RELATIONSHIP]->(target)
    )
    

    Concerning the self relationship, I've hard to understand what you exactly want.

    Chris