AndrewNA
AndrewNA

Reputation: 3

Choose Edge->Vertex based on largest Edge property value

I'm new to Gremlin and Graph DBs, I cannot figure this out yet.

I have V(part) -> E(version) ver: 1..n -> V(attributes). My vertex 'part' can have multiple out edges 'version', with different 'ver' property starting from 1 to n, pointing to different vertex 'attributes'. For instance 'part a' might have two 'version' edges, one with property ver 1 and the other ver 2, each edge point to a different vertex.

For each 'part' in my graph, or just for one, I need to fetch the 'attributes' vertex connected with the highest edge property 'ver' value.

g.addV("part").property("pn", 'p1').as('p').
addV("attributes").property("description", 'Part 1').as('a').
addE('version').property('ver', 1).from('p').to('a')

g.V().has('pn', 'p1').as('p).
addV("attributes").property("description", 'A better Part 1').as('a').
addE('version').property('ver', 2).from('p').to('a')

The logic should be applicable to more complex graphs.

Any help is greatly appreciated. Andrew

Solution, thanks to HadoopMarc.

g.V().hasLabel('part').
local(outE('version').
      order().
        by('ver',desc).
      limit(1)).
inV().
path().
toList()

Upvotes: 0

Views: 49

Answers (1)

Kelvin Lawrence
Kelvin Lawrence

Reputation: 14371

Copying the discussion and updated question to an answer so that others can easily spot the solution. The local step allows (for each starting vertex) the required edge (with the most recent version) to be found:

g.V().hasLabel('part').
local(outE('version').
      order().
        by('ver',desc).
      limit(1)).
inV().
path().
toList()

Upvotes: 1

Related Questions