Change Relationships name

relationship
(Soroosh Nazem) #1

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

0 Likes

(Elaine Rosenberg) #2

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

1 Like

(Ameyasoft) #3

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)

1 Like

(Soroosh Nazem) #4

Thanks Elaine, it worked

0 Likes

(Soroosh Nazem) #5

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

0 Likes