查找两个节点之间的所有路径
使用 gremlin 脚本和 neo4j,我尝试查找两个节点之间的所有路径,最多向下 10 个级别。但我从 REST API 得到的响应只是一个
java.lang.ArrayIndexOutOfBoundsException: -1
这是脚本:
x = g.v(2)
y = g.v(6)
x.both.loop(10){!it.object.equals(y)}.paths
我查看了文档,但找不到与此用例相关的任何内容。
Using a gremlin script and neo4j I try to find all paths between two nodes, descending at most 10 levels down. But all I get as response from the REST API is a
java.lang.ArrayIndexOutOfBoundsException: -1
Here is the script:
x = g.v(2)
y = g.v(6)
x.both.loop(10){!it.object.equals(y)}.paths
I looked through the documentation, but couldnt find anything relevant for this usecase.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Gremlin 中,
loop
的参数是您希望返回的步数,并评估闭包以确定何时跳出循环。在这种情况下,因为您有loop(10)
,所以它会返回到管道未定义的位置。关于闭包,您不仅需要检查该对象是否是有问题的对象(在这种情况下您应该停止),还需要检查是否已经完成 10 次循环。你真正想要的东西是这样的:
但是,我应该补充一点,如果图中有一个循环,那么它会很乐意一遍又一遍地遍历该循环,并导致太多的路径。您可以应用一些巧妙的
filter
和sideEffect
来避免多次访问节点。有关详细信息,请参阅 Gremlin Wiki 上的循环模式页面。
In Gremlin the argument to
loop
is the number of steps back that you wish to go and the closure is evaluated to determine when to break out of the loop. In this case, because you haveloop(10)
it's going to go back way too far to a point where the pipeline is not defined. With the respect to the closure, you'll need to check not only if the object is the one in question, in which case you should stop, but also whether or not you've done 10 loops already.What you really want it something like this:
However, I should add that if there is a cycle in the graph, this will gladly traverse the cycle over and over and result in far too many paths. You can apply some clever
filter
andsideEffect
to avoid visiting nodes multiple times.For more information see the Loop Pattern Page on the Gremlin Wiki.