Change Relationships name

Hello Everyone

I just want to know how I can change the name of relationships in neo4j. I have many relationships that have label "IS_CONNECTED_TO". Right now I want to substitute them all with "KNOWS". Does anybody know how I can do it?

Thanks

You cannot rename a relationship, but you can create a new one and delete the old one.

So you could do something like this:

MATCH (n)-[rel:IS_CONNECTED_TO]->(m)
MERGE (n)-[:KNOWS]->(m)
DELETE rel

If the relationship has properties, then you would need to add them when you merge.

Elaine

4 Likes

Use APOC Refactoring:

MATCH (n)-[rel:IS_CONNECTED_TO]->(m) WITH rel
CALL apoc.refactor.setType(rel, 'KNOWS') YIELD input, output RETURN *

Result:
MATCH (n)-[:KNOWS]->(m)

4 Likes

Thanks Elaine, it worked

Hi Ameyasoft
I am not very familiar yet with APOC. but your advice encouraged me to start and learn APOC. Thank you very mucj

what if in case of condition in where clause in relation ship

Know this is old, but I am trying to change my relation type, with CALL apoc.refactor.setType, but I constantly hit the memory limits or other limits as my data is huge. I am unable to find a batchsize parameter. Any suggestions?

You need to batch the transactions using either call subquery in transitions or apoc.period.iterate.