Search code examples
gremlingraph-databasesamazon-neptune

Traversing a subset of Graph for complex queries


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.


Solution

  • 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