ZaKad
ZaKad

Reputation: 41

Gremlin - sort shortest weighted path output by cost

I try to get the shortest weighted path from Amazon Neptune graph using Gremlin, as demonstrated in TinkerPop recipes -

gremlin> g.V(1).repeat(outE().inV().simplePath()).until(hasId(5)).
           path().as('p').
           map(unfold().coalesce(values('weight'),
                                 constant(0.0)).sum()).as('cost').
           select('cost','p')

But, I need that the output will be ordered by the calculated cost (the lowest cost as the first output) and not by the number of nodes in the path.

I tried a few combinations of order().by(..) in the query without success

Upvotes: 1

Views: 315

Answers (1)

Kelvin Lawrence
Kelvin Lawrence

Reputation: 14371

Does this give you what you need?

g.V(1).repeat(outE().inV().simplePath()).until(hasId(5)).
           path().as('p').
           map(unfold().coalesce(values('weight'),
                                 constant(0.0)).sum()).as('cost').
           order().by(select('cost')).
           select('cost','p')

Upvotes: 0

Related Questions