我有一个庞大的网络(20 * 10 ^ 6个节点),我想返回一条从节点a到节点b的路径,假设长度为4。我的问题是,我还需要显示遍历的子代节点。
我可以轻松找到一条路径:
public Path getPath( final Node nodeA, final Node nodeB, int depth )
{
PathFinder<Path> finder = GraphAlgoFactory.allSimplePaths(
Traversal.expanderForAllTypes(Direction.OUTGOING), depth);
Path path = finder.findSinglePath( nodeA, nodeB );
return path;
}
但是,我也想让每个遍历的节点都具有一级孩子。
问题1是无法添加到路径中。
for (Node n: path.nodes()){
path.add(n.childrens())
}
在Java的neo4j中解决此问题的最佳方法是什么?
要么:
如何实现path.add()?
-我不知道PathExpander应该如何工作;
-我尝试了以下方法,但遇到了问题:
public void expand_path(Node X){
final TraversalDescription TRAVERSAL = Traversal.description()
.breadthFirst()
.relationships(MyTypes.LOVE)
.evaluator(Evaluators.toDepth(1))
.uniqueness(Uniqueness.RELATIONSHIP_GLOBAL);
for (Node n :TRAVERSAL.traverse(X).nodes()){
//HELP!!!
}
}
我也尝试了几个小时的密码查询,但老实说,我不知道如何实现这一点。
提前谢谢大家
最佳答案
将那些子节点添加到路径中没有任何意义。从第二张图可以看出,路径上的节点与子节点一起形成多条路径,而不是一条路径。
我有两件事可能对您的目标有所帮助,
一种是获取路径并像以前一样迭代路径上的节点,对于每个节点,获取所需类型的关系,然后为路径上的节点获取另一端节点(子节点),
for ( Node nodeOnPath : path.nodes() )
{
Relationship rel = nodeOnPath.getSingleRelationship(MyTypes.LOVE, Direction.INCOMING)
Node childNode = rel.getOtherNode(nodeOnPath)
// process the childNode
}
这样,您就可以访问路径上的每个节点及其子节点。
另一个方法是使用遍历框架获取通向末端节点的所有路径,并使用ExtendedPath.extend(path,loveRelationship)将每个路径扩展为类型为“ MyTypes.Love”的其他关系,以将子节点包括到该路径。
具体来说,假设您有两个给定的节点“ a”和“ d”,以及在“ a”和“ d”之间的路径,a
您将获得以下路径和扩展路径
a a a
实现此目的的示例代码如下,
TraversalDescription td = Traversal.description()
.depthFirst()
.relationships(RelTypes.REL, Direction.INCOMING)
.evaluator(new Evaluator() {
@Override
public Evaluation evaluate(final Path path) {
Node endNode = path.endNode();
if ( endNode.getProperty("name").equals('d') ){
return Evaluation.INCLUDE_AND_PRUNE;
}
else {
return Evaluation.INCLUDE_AND_CONTINUE;
}
}
});
for ( Path path : td.traverse(aNode) ) {
Iterator<Relationship> iter = endNode.getRelationships(RelTypes.LOVE,Direction.INCOMING).iterator();
if ( iter.hasNext() ) {
Path extendedPath = ExtendedPath.extend(path, iter.next());
for ( Node enode : extendedPath.nodes() )
System.out.print(enode.getProperty("name") + "---");
}
}
关于java - neo4j中具有一级关系的路径,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20013352/