Search code examples
graph-databasesgremlintinkerpop3amazon-neptune

Gremlin query flatten nested vertices


guys! I'm trying to figure out how can I get all the nested "child" labeled vertices from the following sample data starting from the ID=1 vertex, can someone help me?

gremlin> g.addV("parent").property(id, 1)
gremlin> g.addV("parent").property(id, 2)
gremlin> g.addV("parent").property(id, 3)
gremlin> g.addV("child").property(id, 4)
gremlin> g.addV("child").property(id, 5)
gremlin> g.addV("child").property(id, 6)
gremlin> g.V(1).addE("contains").to(g.V(4))
gremlin> g.V(2).addE("contains").to(g.V(5))
gremlin> g.V(3).addE("contains").to(g.V(6))
gremlin> g.V(1).addE("has").to(g.V(2))
gremlin> g.V(2).addE("has").to(g.V(3))

I want to get the following response:

gremlin> g.V(1)...

==>v[4]
==>v[5]
==>v[6]

Thank you!


Solution

  • Assuming you want to traverse both "has" and "contains", this query will do:

    gremlin> g.V(1).repeat(out()).until(hasLabel("child"))
    ==>v[4]
    ==>v[5]
    ==>v[6]
    

    Starting from V(1) traverse recursively on all out edges, until reaching a "child" vertex.