Question

Suppose I have nodes/edges as :

A --> M  300
A --> B  100
A --> C  200
B --> D  300
B --> E  200
B --> L  1300

i pass source node as A, B then how to display the following output sorted by amount and limit by 2

A --> M 300
A --> C 200

B --> L 1300
B --> D  300 
Was it helpful?

Solution

I'm going to assume you know how to filter vertices "A" and "B" and start the traversal with:

gremlin> g = new TinkerGraph()
==>tinkergraph[vertices:0 edges:0]
... 
gremlin> g.v("A","B").outE.groupBy{it.outV.next()}{[it.amount,it.inV.next()]}.cap.next()
==>v[A]=[[200, v[C]], [100, v[B]], [300, v[M]]]
==>v[B]=[[300, v[D]], [1300, v[L]], [200, v[E]]]

so the above gets you all the "A" and "B" elements grouped with their "amount" (not sure what that number represents in your domain). Once you have that, just use a "reduce" closure to sort and pop-off the first two items in the list:

gremlin> g.v("A","B").outE.groupBy{it.outV.next()}{[it.amount,it.inV.next()]}{it.sort{a,b->b[0]<=>a[0]}[0..<2]}.cap.next()
==>v[A]=[[300, v[M]], [200, v[C]]]
==>v[B]=[[1300, v[L]], [300, v[D]]]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top