I have source node and destination node I want to put restriction on nodes and relation types in the path. I am using Neo4j Java API.
Consider following toy example,
We have three person nodes A, B & C.
Source Node: A & Destination Node: B. There are many other kind of paths may exists between them. I want to restrict paths to specific format like-
(person) -[worksAt]-> (company) -[CompetitorOf]-> (company) <-[worksAt]- (person)
After reading the Neo4j java documentation carefully and experimenting with the code I got following solution working-
PathExpanderBuilder pathExpanderBuilder = PathExpanderBuilder.empty(); pathExpanderBuilder.add(RelationshipType.withName("worksat"), Direction.OUTGOING); pathExpanderBuilder.add(RelationshipType.withName("competitorof"), Direction.BOTH); pathExpanderBuilder.add(RelationshipType.withName("worksat"), Direction.INCOMING); PathExpander<Object> pathExpander pathExpander = pathExpanderBuilder.build();
Once you create a custom
PathExpander you can use it to create appropriate
PathFinder which will filter traversal by the
PathFinder<Path> allPathFinder = GraphAlgoFactory.allSimplePaths(this.pathExpander, 4); Iterable<Path> allPaths = allPathFinder.findAllPaths(sourceNode, targetNode);
In our example
sourceNode would be Node 'A' and
targetNode would be Node 'B'.