简体   繁体   中英

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 (eg a.score==2 , b.score==4 , so choose 'b')

How do I do it?

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]

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