apoc.refactor.categorize

Details

Syntax

apoc.refactor.categorize(sourceKey, type, outgoing, label, targetKey, copiedKeys, batchSize)

Description

Creates new category NODE values from NODE values in the graph with the specified sourceKey as one of its property keys. The new category NODE values are then connected to the original NODE values with a RELATIONSHIP of the given type.

Input arguments

Name

Type

Description

sourceKey

STRING

The property key to add to the on the new node.

type

STRING

The relationship type to connect to the new node.

outgoing

BOOLEAN

Whether the relationship should be outgoing or not.

label

STRING

The label of the new node.

targetKey

STRING

The name by which the source key value will be referenced on the new node.

copiedKeys

LIST<STRING>

A list of additional property keys to be copied to the new node.

batchSize

INTEGER

The max size of each batch.

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 (:Movie {title: 'A Few Good Men', genre: 'Drama'});

A property uniqueness constraint is required to use this procedure: .Create property uniqueness constraint

CREATE CONSTRAINT FOR (n:`Genre`) REQUIRE n.`name` IS UNIQUE

The following examples convert the genre property on the Movie node into a new node with a Genre label and name property. A GENRE relationship from the Movie node to that genre node is also created.

apoc.refactor.categorize
CALL apoc.refactor.categorize('genre', 'GENRE', true, "Genre", "name", [], 100)
MATCH p=()-[:GENRE]->()
RETURN p
Using Cypher’s MERGE and REMOVE clauses
MATCH (n) WHERE n.genre IS NOT NULL
MERGE (n)-[:GENRE]->(:Genre {name: n.genre})
REMOVE n.genre
WITH *
MATCH p=()-[:GENRE]->()
RETURN p
Results
p

(:Movie {title: 'A Few Good Men'})-[:GENRE]→(:Genre {name: 'Drama'})

apoc.refactor.categorize.usage
Figure 1. New graph structure