apoc.refactor.rename.type

Details

Syntax

apoc.refactor.rename.type(oldType, newType [, rels, config ]) :: (batches, total, timeTaken, committedOperations, failedOperations, failedBatches, retries, errorMessages, batch, operations, constraints, indexes)

Description

Renames all RELATIONSHIP values with type oldType to newType. If a LIST<RELATIONSHIP> is provided, the renaming is applied to the RELATIONSHIP values within this LIST<RELATIONSHIP> only.

Input arguments

Name

Type

Description

oldType

STRING

The type to rename.

newType

STRING

The new type for the relationship.

rels

LIST<RELATIONSHIP>

The relationships to apply the new name to. If this list is empty, all relationships with the old type will be renamed. The default is: [].

config

MAP

{ batchSize = 100000 :: INTEGER, concurrency :: INTEGER, retries = 0 :: INTEGER, parallel = true :: BOOLEAN, batchMode = "BATCH" :: STRING }. The default is: {}.

Return arguments

Name

Type

Description

batches

INTEGER

The number of batches the operation was run in.

total

INTEGER

The total number of renamings performed.

timeTaken

INTEGER

The time taken to complete the operation.

committedOperations

INTEGER

The total number of committed operations.

failedOperations

INTEGER

The total number of failed operations.

failedBatches

INTEGER

The total number of failed batches.

retries

INTEGER

The total number of retries.

errorMessages

MAP

The collected error messages.

batch

MAP

{ total :: INTEGER, failed :: INTEGER, committed :: INTEGER, errors :: MAP }

operations

MAP

{ total :: INTEGER, failed :: INTEGER, committed :: INTEGER, errors :: MAP }

constraints

LIST<STRING>

Constraints associated with the given label or type.

indexes

LIST<STRING>

Indexes associated with the given label or type.

Refactoring nodes using Cypher

Node labels and relationship types can be referenced dynamically in Cypher without using APOC.

Cypher syntax for creating, matching and merging labels and types dynamically
CREATE (n1:$(label))-[r:$(type)]->(n2:$(label))
MERGE (n1:$(label))-[r:$(type)]->(n2:$(label))
MATCH (n1:$(label))-[r:$(type)]->(n2:$(label))

The dynamically calculated type must evaluate to a STRING or LIST<STRING>. For more information, see the Cypher Manual → CREATE, MERGE, MATCH.

Batching, as well as parallel execution, can be achieved in Cypher using CALL {…​} IN CONCURRENT TRANSACTIONS. For more information, see CALL subqueries in transactions → Concurrent transactions.

Usage Examples

The examples in this section are based on the following sample graph:

CREATE (mark:Engineer {name: "Mark", city: "London"})
CREATE (jennifer:Engineer {name: "Jennifer", city: "St Louis"})
CREATE (michael:Engineer {name: "Michael", city: "Dresden"})
CREATE (jim:Engineer {name: "Jim", city: "London"})
CREATE (alistair:Engineer {name: "Alistair", city: "London"})

MERGE (jim)-[:COLLEAGUES {since: date("2006-05-01")}]->(alistair)
MERGE (mark)-[:COLLEAGUES {since: date("2018-02-01")}]->(jennifer)
MERGE (mark)-[:COLLEAGUES {since: date("2013-05-01")}]->(michael);

The following changes the relationship type between Jim and Alistair from COLLEAGUES to FROLLEAGUES:

apoc.refactor.rename.type
MATCH (:Engineer {name: "Jim"})-[rel]->(:Engineer {name: "Alistair"})
WITH collect(rel) AS rels
CALL apoc.refactor.rename.type("COLLEAGUES", "FROLLEAGUES", rels)
YIELD batches
RETURN count(*) AS count
Using Cypher
MATCH (:Engineer {name: "Jim"})-[rel:COLLEAGUES]->(:Engineer {name: "Alistair"})
CALL (rel) {
    WITH startNode(rel) AS startNode, endNode(rel) AS endNode
    CREATE (f)-[r:FROLLEAGUES]->(b)
    SET r = properties(rel)
    DELETE rel
}
RETURN count(*) AS count
Results
count

1

After this query has run, we’ll have the following graph:

apoc.rename rename rel type

APOC is not inverting the type; instead, it adds a new relationship with the desired direction and deletes the original.