Bala
Bala

Reputation: 367

Gremlin Traversal

EDITED: After @Prashant answer I understood order of my output needs to be preserved, so accordingly I have edited my original question and reposted.

Below is my graph: enter image description here

g.addV('person').property(id, 1)
g.addV('person').property(id, 2)
g.addV('person').property(id, 3)
g.addV('person').property(id, 4)
g.addV('person').property(id, 5)
g.addV('person').property(id, 6)
g.addV('person').property(id, 7)
g.addV('person').property(id, 8)
g.addV('person').property(id, 9)
g.addV('person').property(id, 10)
g.addV('person').property(id, 11)
g.addV('person').property(id, 12)
g.addV('product').property(id, 13)
g.V(2).addE('related').to(V(1))
g.V(2).addE('related').to(V(5))
g.V(5).addE('related').to(V(6))
g.V(5).addE('related').to(V(7))
g.V(5).addE('related').to(V(9))
g.V(7).addE('related').to(V(8))
g.V(9).addE('related').to(V(4))
g.V(9).addE('related').to(V(10))
g.V(4).addE('related').to(V(3))
g.V(10).addE('related').to(V(11))
g.V(10).addE('related').to(V(12))
g.V(1).addE('chose').to(V(13))
g.V(8).addE('chose').to(V(13))
g.V(9).addE('chose').to(V(13))
g.V(3).addE('chose').to(V(13))
g.V(11).addE('chose').to(V(13))
g.V(12).addE('chose').to(V(13))

I want to traverse from root node (2) to leaf nodes (1, 6, 8, 3, 11 and 12). While such traversal I want to fetch nodes which are connected to product node 13 ie) I wish to write a query which returns 1, 8, 9 3, 11 and 12.

@Prashant's answer:

g.V().repeat(out().simplePath()).until(out().hasLabel("product")).dedup()

outputs

==>v[1]
==>v[9]
==>v[8]
==>v[3]
==>v[11]
==>v[12]

However order of my required output is 1, 8, 9, 3, 11 and 12 ie) at each level, requirement is to pick a child node and traverse all the way to its leaf node. Order of nodes at each level of the tree is guaranteed to be in ascending order. Any help here, please? Thanks!!

Upvotes: 2

Views: 84

Answers (1)

PrashantUpadhyay
PrashantUpadhyay

Reputation: 877

Below query should do the job.

g.V().repeat(out().simplePath()).until(out().hasLabel("product")).dedup()

Upvotes: 1

Related Questions