在仔细阅读了Neo4j java 文档并尝试了代码之后,我得到了以下解决方案 -
PathFinder
通过使用创建自定义PathExpander
来过滤探索的路径PathExpanderBuilder
。
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();
创建自定义PathExpander
后,您可以使用它来创建适当PathFinder
的过滤器遍历PathFinder
。
PathFinder<Path> allPathFinder = GraphAlgoFactory.allSimplePaths(this.pathExpander, 4);
Iterable<Path> allPaths = allPathFinder.findAllPaths(sourceNode, targetNode);
在我们的示例sourceNode
中,节点“A”targetNode
是节点“B”。