Search code examples
gremlintinkerpopjanusgraphamazon-neptune

gremlin get all nodes which are directly or indirectly connected for a given node


I'm newbie to gremlin.

I'm trying to get all nodes which are connected(directly or indirectly) to a given vertices.
ex:
one cluster like this in a graph.
A-D
A-F
F-B
B-C
B-G
G-H
G-I

second cluster like this in a graph.
J-k
J-L

My Requirement when I search for the node "B", I need to get the all connected vertices for B (no edge direction) only.
ex output: A,D,F,B,C,G,H,I

When I search for J.
ex outpu:J,k,L

Any help would be appreciated, Thanks in advance.


Solution

  • It's as simple as:

    g.V('B').emit().repeat(both().dedup()).toSet()
    

    You basically traverse in both directions and emit every vertex you see until there's no new vertex found.