0
votes

Is it possible to write a Gremlin query, given a vertex, recursively traverse the whole graph connected to that node?

For example,

      watched           director
user1 -------> movie_1 <------ chris nolan
                 ^
user2------------|  
      watched

Given movie_1, return all vertices connected to movie_1 (user1, user2, chris nolan), then return vertices connected to user1, user2, chris nolan and so on.

1

1 Answers

4
votes

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.