Search code examples
gremlindatastax-enterprise-graph

How do I make the following subquery in Gremlin?


For example, using the Tinkerpop's toy graph data (graph = TinkerFactory.createModern()), I want to do something like the following:

g.V().hasLabel('person').has('name', 'marko').project('a', 'b').by().by(...)

I want to use a property of the vertices from the first traversal and use that in the query inside second by().

Something like this pseudocode:

by(__.V().has(hasLabel('person').has('name', [property-from-first-traversal])))

This might be easier to do in separate queries, but I want to do it in one query - something like a Subquery in SQL.


Solution

  • You're probably looking for something like this:

    gremlin> g = TinkerFactory.createModern().traversal()
    ==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
    gremlin> g.addV('person').property('name','marko')
    ==>v[13]
    gremlin> g.V().has('person','name', 'marko').
               project('a', 'b').
                 by().
                 by(__.as('x').V().hasLabel('person').where(eq('x')).by('name').count())
    ==>[a:v[1],b:2]
    ==>[a:v[13],b:2]
    

    However, be careful with where() filters, thus far no provider (that I am aware of) will turn this into an index lookup, hence it will be a scan over all person vertices in your graph.