I asked this question How to get subgraph a while back and found while it does give me all nodes and relationships at the Nth level, the answers assume the direction of the relationships either fan out from the starting node or converge to the node. What I am looking for is a complete subgraph that captures all relationships and nodes at a certain level while preserving and relating the direction of the relationships.
For example, if I want all nodes and relationships at depth 2 I can make a directionless query as follows:
START n=node(12345)
MATCH (n)<-[r*1..2]->(m)
RETURN r, m;
This works well as I get all nodes and all relationships, but I find I have no idea what direction the relationships are in.
If I limit the search to a depth of 1 and perform two searches, one incoming and one outgoing, I can get all nodes and relationships with direction that way. I can then recursively do the same query for all nodes found at depth two, discarding any relationships that contain nodes not found beyond the 2nd level from the start. This seems rather painful and manual but it works.
I've also tried using the TraversalDescription framework in the embedded Java API, but that doesn't quite return all relationships. For example, the following snippet gave me all nodes at depth 2, but it missed some relationships between nodes at depth 2:
for ( Path position : graphDb.traversalDescription()
.breadthFirst()
.evaluator( Evaluators.toDepth( 2 ) )
.traverse( templateNode ) )
{
output += position + "\n";
}
Is there an easy way to do this without multiple manual iterations?