Edward Brown
Edward Brown

Reputation: 109

Gremlin: How to select() multiple vertices into a collection then get the one with the highest property value

Take a traversal like this:

g.V().as('a')......has(name,'test').as('b').....select('a','b')

At this point, now I've stored and selected out 'a' and 'b', I want to identify the one with the high property value (e.g. a.score==2, b.score==4, so choose 'b')

How do I do it?

Upvotes: 3

Views: 4836

Answers (1)

Daniel Kuppitz
Daniel Kuppitz

Reputation: 10904

It's easier if you give every candidate on the path the same label:

g.V().as('a')....
  has('name,'test').as('a').
  select(all, 'a').
  order(local).
    by('score', decr).
  limit(local, 1)

Here's how it looks on the modern toy graph:

gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().as('a').out('knows').as('a').select(all, 'a')
==>[v[1],v[2]]
==>[v[1],v[4]]
gremlin> g.V().as('a').out('knows').as('a').
......1>   select(all, 'a').
......2>     by(unfold().valueMap(true).fold())
==>[[label:person,name:[marko],age:[29],id:1],[label:person,name:[vadas],age:[27],id:2]]
==>[[label:person,name:[marko],age:[29],id:1],[label:person,name:[josh],age:[32],id:4]]

At this point we know that the expected result is v[1] (29 > 27) for the first path and v[4] (32 > 29) for the second path.

gremlin> g.V().as('a').out('knows').as('a').
......1>   select(all, 'a').
......2>   order(local).
......3>     by('age', decr).
......4>   limit(local, 1)
==>v[1]
==>v[4]

Upvotes: 6

Related Questions