If you want to collect both the nodes and relationships along all the paths, it will be much easier to use the apoc.path.subgraphAll() procedure. It eliminates trying to build all the collection with multiple 'unwind's and 'collect distinct'. The following should give you the nodes and relationships along the forward paths. the subgraphAll procedure is configured to only traverse 'Asset' and 'Event' nodes along outgoing 'TRANSFORMS_INTO' and outgoing 'HAS_EVENT' relationships.
MATCH (n:ASSET{uniqueID: "UNIQUE_ID"})
CALL apoc.path.subgraphAll(n, {
relationshipFilter: "TRANSFORMS_INTO>|HAS_EVENT>",
nodeFilter: "Asset|Event"
})
YIELD nodes, relationships
RETURN nodes, relationships
You can get the backward paths by switching the relationship filter on the 'TRANSFORMS_INTO' relationship to incoming.
MATCH (n:ASSET{uniqueID: "UNIQUE_ID"})
CALL apoc.path.subgraphAll(n, {
relationshipFilter: "<TRANSFORMS_INTO|HAS_EVENT>",
nodeFilter: "Asset|Event"
})
YIELD nodes, relationships
RETURN nodes, relationships
These queries mix the 'Asset' and 'Event' nodes into the same collection. If you want the 'Event' nodes in a separate collection, you can use this version (forward path):
MATCH (n:ASSET{uniqueID: "UNIQUE_ID"})
CALL apoc.path.subgraphAll(n, {
relationshipFilter: "TRANSFORMS_INTO>",
nodeFilter: "Asset"
})
YIELD nodes, relationships
CALL {
with nodes
unwind nodes as asset
optional match (asset)-[:HAS_EVENT]->(e:Event)
return collect(e) as events
}
return nodes as assets, relationships, events
If you want the 'Event' and its corresponding relationship, you can collect them in a map as follows:
MATCH (n:ASSET{uniqueID: "UNIQUE_ID"})
CALL apoc.path.subgraphAll(n, {
relationshipFilter: "TRANSFORMS_INTO>",
nodeFilter: "Asset"
})
YIELD nodes, relationships
CALL {
with nodes
unwind nodes as asset
optional match (asset)-[r:HAS_EVENT]->(e:Event)
return collect({event: e, rel: r}) as events
}