You would likely use some variation of repeat()
step to accomplish what you want (i.e. traverse an arbitrary number of steps away from a starting vertex). When asking questions about Gremlin it's nice to have asciiart diagrams of the graph but it's even better to have a simple Gremlin script that creates the graph itself like this:
g.addV('movie').property('name','movie-1').as('m').
addV('user').property('name','user-1').as('u1').
addV('user').property('name','user-2').as('u2').
addV('person').property('name','chris nolan').as('d').
addE('watched').from('u1').to('m').
addE('watched').from('u2').to('m').
addE('directed').from('d').to('m').iterate()
Then, to start at "movie-1" and traverse to arbitrary depth, just do:
gremlin> g.V().has('movie','name','movie-1').
......1> repeat(__.in()).
......2> emit().
......3> valueMap(true)
==>[id:2,label:user,name:[user-1]]
==>[id:4,label:user,name:[user-2]]
==>[id:6,label:person,name:[chris nolan]]
That will continue to traverse on incoming edges until it hits vertices without any, emitting all vertices found along the way. Obviously, if you your edges don't all traverse in, then you'll need to alter the pattern within repeat()
to traverse both()
, but you'll want to try to avoid cycles in the process or the repeat()
will traverse indefinitely. Use of simplePath()
might be helpful there but ultimately your approach to loop termination will be defined by your graph structure.
Note that this could be an expensive query depending on the traversal depth.