Using gremlin to find all node s at a given distance from the start node

I am trying to use gremlin via java / pipe and as one of my first requests, I am trying to find all nodes that are reachable from a given starting node with a maximum distance of 3. In cypher, my request 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 still have in the gremlin:

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

      

which is not working properly. From the way I understand it, it should emit the output of every iteration and repeat three times. I am getting too few results at the moment.

Any help would be appreciated,

Thank.

+3


source to share


1 answer


If your start node is gv (1), then find all unique nodes in three steps:

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

      

.. you might have to do this & lt; 4 (forget it). Then if your start node has idd = v1 then do the following:



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

      

Make sure you have an index on idd or something else that is a linear scan through all gVs for those vertices that have idd = v1.

NTN, Marco.

+2


source







All Articles