开发者

Find all paths between two nodes

开发者 https://www.devze.com 2023-04-08 10:41 出处:网络
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

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)}.p开发者_运维知识库aths

I looked through the documentation, but couldnt find anything relevant for this usecase.


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 have loop(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:

x.both.loop(1){!it.object.equals(y) && it.loops < 10}.paths

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 and sideEffect to avoid visiting nodes multiple times.

For more information see the Loop Pattern Page on the Gremlin Wiki.

0

精彩评论

暂无评论...
验证码 换一张
取 消