Search code examples
gremlinamazon-neptune

Gremlin - select vertices based on value found in traversal


I want to use the id of a vertex that has prop_a=x to find other vertices which have this value in some other property.
Something like:

g.V().sideEffect(has('prop_a','x').id().as('val')).has('prop_b',__.select('val')).count()

But the above doesn't return the right result.
In addition, I need it to run efficiently on AWS Neptun.


Solution

  • Queries to create the data:

    gremlin> g.addV().property(id,1).property('prop_a','x')
    ==>v[1]
    gremlin> g.addV().property(id,2).property('prop_b',1)
    ==>v[2]
    

    Query to get the desired data:

    gremlin> g.V().
    ......1>   has('prop_a', 'x').
    ......2>   id().as('val').
    ......3>   V().as('b').
    ......4>   values('prop_b').
    ......5>   where(eq('val')).
    ......6>   select('b')
    ==>v[2]