Search code examples
gremlintinkerpoptinkerpop3

how to get an an edge which has the maximum value of a edge property from a vertex


Scenario: Find which is the job with most experience of an employee Vertex which has out edge WorkedAs and has an edge property workedForYears which has the experience in years for that job.

I have used

g.V().has('EmployeeId','1234').outE('WorkedAs').values('workedForYears').max()

This would return me an integer. Is there any way I can get the edge?


Solution

  • You could use order().by().limit(1) pattern rather than max() so you don't transform the items in the traversal into an integer.

    g.V().has('EmployeeId','1234').outE('WorkedAs').as('e') \
         .order().by('workedForYears', decr).limit(1).select('e')
    

    Notice that I used the as() step (As Step docs) to label a particular spot in the Gremlin traversal. Then after other processing, you can jump back to that particular spot by using select().