I have a somewhat unique scenario. I am going to get a node A. From node A, I want to find path to some other nodes (say set C of nodes). Once I get these set of paths I want to rerun those paths from the other node B (which corresponds/similar to node A) to discover all nodes (which corresponds to set C of nodes) which follow same paths (as from node A to nodes in set C).
For example if for (A)
I have got (E),(G)
following these paths:
---[:Rel2]--->(F)---[:Rel1]--->(G)
/
(A)---[:Rel1]--->(E)
Then for (B)
, I should get (J),(H)
following the same paths, but should not get (K)
as it does not follow any of the path patterns returned for (A):
---[:Rel2]--->(I)---[:Rel1]--->(J)
/
(B)---[:Rel1]--->(H)
\
---[:Rel2]--->(K)
As of now, I am doing it as follows:
- Given A, run the initial cypher to get the desired paths.
- The paths are returned as instances of
org.neo4j.driver.v1.types.Path
. EachPath
instance is essentially a list oforg.neo4j.driver.v1.types.Path.Segment
instances. So iterate overSegment
list to form cypher string for each corresponding path. - Use the cypher strings to do
MATCH
from B.
This is working, however, forming cypher string from the Java lists of Segment
classes gives me a weird feeling. I dont know if this approach is correct or not. Is there any other better looking approach? Or is this approach just ok? Is there any recommended approach? Have anyone came across similar scenario?
Edit: You may view first graph as a part of ("part" because it does not have schema definition corresponding to instance subpath (B)---[:Rel2]--->(K)
) a schema and second graph as an instance of that schema. We first find the path in schema and then run it on instance.