After reading the apoc documentation I found apoc.path.expandConfig function that almost solved my problem.
So the solution is:
- get paths to nodes that are Element or Leaf
- get paths to system nodes
- for path get relation return and return segment of the path as: (start)-[relationship]-(end)
- union those segments to remove duplicates
It is working and my next task is to test performance with large graphs. Any thoughts? @Nobody @TheCrusher
match (start: System)
where id(start)=1
call apoc.path.expandConfig(start, { labelFilter: 'Element|Leaf' }) yield path
unwind relationships(path) as x
return startnode(x) as s, x as r, endnode(x) as e
union
match (start: System)
where id(start)=1
call apoc.path.expandConfig(start, { labelFilter: '>System' }) yield path
unwind relationships(path) as x
return startnode(x) as s, x as r, endnode(x) as e
EDIT: I came up with another query which does the work:
match (s: System)-[r*]-(m)
where id(s)=1 and (m:Element or m:Leaf or m:System) and not (s)-[*]-(:System)-[*]-(m)
unwind r as r1
with distinct r1 as rd
return startnode(rd), rd, endnode(rd)