apoc.refactor.categorize
Syntax |
|
||
Description |
Creates new category |
||
Input arguments |
Name |
Type |
Description |
|
|
The property key to add to the on the new node. |
|
|
|
The relationship type to connect to the new node. |
|
|
|
Whether the relationship should be outgoing or not. |
|
|
|
The label of the new node. |
|
|
|
The name by which the source key value will be referenced on the new node. |
|
|
|
A list of additional property keys to be copied to the new node. |
|
|
|
The max size of each batch. |
Refactoring nodes using Cypher
Node labels and relationship types can be referenced dynamically in Cypher without using APOC.
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 (:Movie {title: 'A Few Good Men', genre: 'Drama'});
We want to move the genre
from the Movie
node to a new node with the Genre
label and name
property.
We’ll also create a GENRE
relationship from the Movie
node to that genre node.
This procedure requires us to create a unique constraint on the Genre
label, name
property, otherwise we’ll get the following exception:
CALL apoc.refactor.categorize('genre', 'GENRE', true, "Genre", "name", [], 100);
Failed to invoke procedure `apoc.refactor.categorize`: Caused by: java.lang.IllegalArgumentException: Before execute this procedure you must define an unique constraint for the label and the targetKey:
CREATE CONSTRAINT FOR (n:`Genre`) REQUIRE n.`name` IS UNIQUE
Once we’ve created the constraint, we can re-run the procedure, and then see the new graph structure by running the following query:
MATCH p=()-[:GENRE]->()
RETURN p;
Alternatively, the same can be achieved using only Cypher, without having to first create a constraint:
MATCH (n) WHERE n.genre IS NOT NULL
MERGE (n)-[:GENRE]->(:Genre {name: n.genre})
REMOVE n.genre
