0
votes

I'm fairly new to Gremlin and I'm trying to make a query more efficient.

With a graph that looks something like this:

[vertex: label=a] -> [vertex: label=b] -> [vertex: label=c] -> [vertex: label=d]

I need to traverse the graph, and get the results for each step of the traversal.

For example, from my base Vertex I may need to get everything related to it with the label "b" and everything related to "b" with the label "c" and so on.

I can do the requests individually like this:

results1 = g.V('someid').out().hasLabel('b').toList()
results2 = g.V('someid').out().hasLabel('b').out().hasLabel('c').toList()
results3 = g.V('someid').out().hasLabel('b').out().hasLabel('c').out().hasLabel('d').toList()

but that seems counter intuitive, and the response time starts to stack up.

I'm not really sure what I'm looking for, but maybe someway to continue searching a subset of the graph rather than starting from the base vertex every time.

EDIT:

I found the Union step, but this seems to combine multiple small queries into one big query and I believe ends up with the same inefficiencies as doing all the queries individually.

1
Trying to make sure I fully understand your question. Are you looking for a way to start a future traversal from places you have found before? If so you can pass a list of IDs to the V() step as one option.Kelvin Lawrence
Sorry if I was't super clear. When starting the query I have a single ID, which I pass to V(). I then need to recursively get relationships for that vertex. So I need V()->A and V()->A->B and V()->A->B->C etcTKems

1 Answers

0
votes

To get back the path of where your query went is quite straightforward in Gremlin. Here is a simple example using a data set that models airline routes:

g.V('3').  
  repeat(out().hasLabel('airport').simplePath()).
  until(has('code','AGR')).
  limit(5).
  path().
    by('code')

==>[AUS,JFK,BOM,AGR]
==>[AUS,YYZ,BOM,AGR]
==>[AUS,LHR,BOM,AGR]
==>[AUS,FRA,BOM,AGR]
==>[AUS,EWR,BOM,AGR] 

You will find some related examples here: http://www.kelvinlawrence.net/book/PracticalGremlin.html http://www.kelvinlawrence.net/book/PracticalGremlin.pdf