Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using gremlin to find all nodes within a given distance from the start node

I'm attempting to use gremlin through java/pipes and as one of my first queries I'm trying to find all nodes which are reachable from a given start node with a maximum distance of 3. In cypher my query is:

START n = node(*)
MATCH n -[*1..3]-> reached 
WHERE (has(n.id) and n.id = \"v1\")
RETURN distinct n, reached

which works correctly, and what I have so far in gremlin is:

_().has('idd', 'v1').out().loop(1){it.loops < 3}{true}

which does not work correctly. From the way I understand it, it should emit the output from each iteration and iterate 3 times. At the moment I'm getting too few results.

Any help would be appreciated,

Thanks.

like image 599
user1690293 Avatar asked Sep 03 '25 03:09

user1690293


1 Answers

If your start node is g.v(1), then to find all unique nodes three steps away do:

g.v(1).out.loop(1){it.loops < 3}{true}.dedup

..you might have to make it < 4 (forget). Next, if your start node has idd=v1, then do:

g.V('idd','v1').out.loop(1){it.loops < 3}{true}.dedup

Be sure you have an index on idd or else that is a linear scan through all g.V for those vertices that have idd=v1.

HTH, Marko.

like image 169
Marko A. Rodriguez Avatar answered Sep 05 '25 00:09

Marko A. Rodriguez