Collapse nodes to relationships

The APOC library contains a procedure that can be used to collapse a node into a relationship.

Procedure for collapsing nodes

Qualified Name Type

apoc.refactor.collapseNode(nodes ANY, relType STRING) - collapses the given NODE and replaces it with a RELATIONSHIP of the given type.



The example below will further explain this procedure.

The following creates a graph containing a Flight node and two Airport nodes (origin and destination)
CREATE (flight:Flight {number: "BA001"})
CREATE (origin:Airport {code: "LHR"})
CREATE (destination:Airport {code: "AMS"})
CREATE (flight)<-[:OUT]-(origin)
CREATE (flight)-[:IN]->(destination)
The following query collapses the Flight node, replacing it with a CONNECTED to relationship:
MATCH (flight:Flight {number: "BA001"})
CALL apoc.refactor.collapseNode([flight],'CONNECTED_TO')
YIELD input, output , error
RETURN input, output, error

If the above query is run, it will result in the following graph: