apoc.refactor.extractNode

Details

Syntax

apoc.refactor.extractNode(rels, labels, outType, inType) :: (input, output, error)

Description

Expands the given RELATIONSHIP VALUES into intermediate NODE VALUES. The intermediate NODE values are connected by the given outType and inType.

Input arguments

Name

Type

Description

rels

ANY

The relationships to turn into new nodes. Relationships can be of type STRING (elementId()), INTEGER (id()), RELATIONSHIP, or LIST<STRING | INTEGER | RELATIONSHIP>.

labels

LIST<STRING>

The labels to be added to the new nodes.

outType

STRING

The type of the outgoing relationship.

inType

STRING

The type of the ingoing relationship.

Return arguments

Name

Type

Description

input

INTEGER

The internal id of the original entity.

output

NODE

The copied entity.

error

STRING

Any error that occurred during the copy process.

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.

Usage Examples

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

CREATE (origin:Airport {code: "LHR"})
CREATE (destination:Airport {code: "AMS"})
CREATE (origin)-[:FLIGHT {number: "BA001"}]->(destination);

The following creates a Flight node with an IN relationship from LHR and an OUT relationship to AMS:

apoc.refactor.extractNode
MATCH (:Airport)-[rel:FLIGHT]->(:Airport)
WITH collect(rel) AS rels
CALL apoc.refactor.extractNode(rels,['Flight'],'OUT','IN')
YIELD input, output
RETURN input, output;
Using Cypher
MATCH (:Airport)-[rel:FLIGHT]->(:Airport)
CALL (rel) {
    WITH startNode(rel) AS startNode, endNode(rel) AS endNode, rel
    CREATE (startNode)-[:IN]->(f:Flight)-[:OUT]->(endNode)
    SET f = properties(rel)
    DELETE rel
}
RETURN 0 AS input, newNode AS output
Results
input output

0

(:Flight {number: "BA001"})

We can list all the Flight nodes by running the following query:

MATCH path = (origin)-[:IN]->(:Flight)-[:OUT]->(destination)
RETURN path;
Results
path

(:Airport {code: "LHR"})-[:IN]→(:Flight {number: "BA001"})-[:OUT]→(:Airport {code: "AMS"})