MATCH (a:NodeA { `value`: 9 })
MATCH (b:NodeB { `othervalue`: 45 })
WITH a,b
MATCH p = ()-[*1..5]-() WHERE (a in nodes(p) OR b in nodes(p))
RETURN DISTINCT(p)
The idea is to return all the different graphs in the database, that has any of those nodes present.
The query seems to work, but is killing the server.
Is there any better way to re-write the query in a more performant way?
Maybe using APOC?
Note that this will give you back all relationships between all reachable nodes (up to depth 5), so it's possible that if there are two reachable nodes at distance 5 that are connected, you'll get the relationship between them too.
Does this work for you, or are you looking for something else?
You may need to explain what you mean by "returning graphs". Your query is returning paths, not graphs. The apoc call is returning a single row with a collection of reachable nodes and the collection of relationships between those nodes, and that combination represents the subgraph reachable at the given distance from the starting node.
I can reproduce what you're seeing, but looking at the code returned (and running this to UNWIND on the lists) I can see that it does have the proper results being returned. The query itself is correct and returning the correct values: 1 row per a node, and for each row a separate list of nodes and relationships from the expansion.
This is looking like a bug in the browser when displaying nodes and relationships that are in a list, when we have lists across multiple rows, though I can't quite isolate which logical case it's getting tripped up at.
In the meantime, as a workaround, when you know you have multiple starting nodes and not just one, you can collect the starting nodes first and use that when making the APOC call. That will result in just a single row with a single list of nodes reachable by either node, and relationships reachable by either node. The browser/visualizer isn't tripping on that.
MATCH (a:MyNode { `value`: 9 })
WITH collect(a) as startNodes
CALL apoc.path.subgraphAll(startNodes, {maxLevel:1}) YIELD nodes, relationships
RETURN nodes, relationships;