简体   繁体   中英

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM