I'm trying to optimize my code, but I'm newbie in Gremlin and I hope you can help me.
Your code below is very clean. You are using all the right techniques.
g.v('#6:0').as('startPoint').outE('knows').inV.loop('startPoint'){it.loops<=3 && !(
it.object.name in ['A','B'])}.has('name', 'B').path
This query gives me a list of paths (I'm using gremlin in Java with orientDB) but I just need the path score, which is given by the sum of edges' weights. Right now I'm doing this by iterating all the paths and sum the edges' weights, but this is too heavy because I have a lot of paths, nodes, etc. So I'm trying to calculate the weights as I traverse the graph using gremlin, but I can't figure it out. Can you help me with the query?
Will this work for you?
g.v('#6:0').as('startPoint').outE('knows').inV.loop('startPoint'){it.loops<=3 && !(it.object.name in ['A','B'])}.has('name', 'B').path.transform{
it.findAll{ it instanceof Edge}.collect{ it.weight }.sum()
}
Using the toy TinkerGraph:
gremlin> g.v(1).outE.inV.outE.inV.path.transform{it.findAll{it instanceof Edge}}
==>[e[8][1-knows->4], e[10][4-created->5]]
==>[e[8][1-knows->4], e[11][4-created->3]]
gremlin> g.v(1).outE.inV.outE.inV.path.transform{it.findAll{it instanceof Edge}.collect{it.weight}}
==>[1.0, 1.0]
==>[1.0, 0.4]
gremlin> g.v(1).outE.inV.outE.inV.path.transform{it.findAll{it instanceof Edge}.collect{it.weight}.sum()}
==>2.0
==>1.4000000059604645
...if you want the path and weight together:
gremlin> g.v(1).outE.inV.outE.inV.path.transform{[it,it.findAll{it instanceof Edge}.collect{it.weight}.sum()]}
==>[[v[1], e[8][1-knows->4], v[4], e[10][4-created->5], v[5]], 2.0]
==>[[v[1], e[8][1-knows->4], v[4], e[11][4-created->3], v[3]], 1.4000000059604645]
HTH,
Marko.