8.5. Graph Refactorings

This section describes graph refactoring procedures in the APOC library.

Given the agile approach taken towards building graph models, graph refactoring (or migration) is something that all graph based projects will have to do at some stage. While many graph refactorings can be done using Cypher directly, the APOC library provides tools to make the process easier.

The library contains procedures for the following high level refactorings

The available algorithms are described in the table below:

call apoc.refactor.cloneNodes([node1,node2,…​])

clone nodes with their labels and properties

call apoc.refactor.cloneNodesWithRelationships([node1,node2,…​])

clone nodes with their labels, properties and relationships

call apoc.refactor.cloneSubgraph([node1,node2,…​], [rel1,rel2,…​]=[], {standinNodes:[[oldNode1, standinNode1], …​], skipProperties:[prop1, prop2, …​]}={}) YIELD input, output, error

clone nodes with their labels and properties (optionally skipping any properties in the skipProperties list via the config map), and clone the given relationships (will exist between cloned nodes only). If no relationships are provided, all relationships between the given nodes will be cloned. Relationships can be optionally redirected according to standinNodes node pairings (this is a list of list-pairs of nodes), so given a node in the original subgraph (first of the pair), an existing node (second of the pair) can act as a standin for it within the cloned subgraph. Cloned relationships will be redirected to the standin.

call apoc.refactor.cloneSubgraphFromPaths([path1,path2,…​], {standinNodes:[[oldNode1, standinNode1], …​], skipProperties:[prop1, prop2, …​]}={}) YIELD input, output, error

from the subgraph formed from the given paths, clone nodes with their labels and properties (optionally skipping any properties in the skipProperties list via the config map), and clone the relationships (will exist between cloned nodes only). Relationships can be redirected according to optional standinNodes node pairings (this is a list of list-pairs of nodes), so given a node in the original subgraph (first of the pair), an existing node (second of the pair) can act as a standin for it within the cloned subgraph. Cloned relationships will be redirected to the standin.

call apoc.refactor.mergeNodes([node1,node2])

merge nodes onto first in list

call apoc.refactor.mergeRelationships([rel1,rel2,…​],{config})

merge relationships onto first in list

call apoc.refactor.to(rel, endNode)

redirect relationship to use new end-node

call apoc.refactor.from(rel, startNode)

redirect relationship to use new start-node

call apoc.refactor.invert(rel)

inverts relationship direction

call apoc.refactor.setType(rel, 'NEW-TYPE')

change relationship-type

call apoc.refactor.extractNode([rel1,rel2,…​], [labels], 'OUT','IN')

extract node from relationships

call apoc.refactor.collapseNode([node1,node2],'TYPE')

collapse nodes with 2 rels to relationship, node with one rel becomes self-relationship

call apoc.refactor.normalizeAsBoolean(entity, propertyKey, true_values, false_values)

normalize/convert a property to be boolean

call apoc.refactor.categorize(node, propertyKey, type, outgoing, label)

turn each unique propertyKey into a category node and connect to it