Gremlin's request for the sum of 2 or more values
I need to write a gremlin query that can sum 2 or more properties in a set of vertices and return them as separate values.
Statement 1:
g.V().has('label1').values('p1').sum()
This will return the sum of the p1 values โโof all the vertices of 'label1' - say 100
Statement 2:
g.V().has('label1').values('p2').sum()
This will return the sum of the p2 values โโof all the vertices of 'label1' - say 200
Statement 3:
g.V().has('label1').values('p1','p2').sum()
Tried the above statement, but as expected it won't work, it will return 300, the sum of all p1 and p2 properties 'label1'
I need a query that will return the sum of p1 and the sum of p2 into one result. Perhaps there is a way that I could "collapse" or collapse a set of vertices into one vertex and aggregate the properties according to certain rules ... in my case for the sum.
source to share
You can group all values โโby their key:
g.V().has("label1").properties("p1","p2").
group().by(key).by(value().sum())
EDIT
Proof that it works:
gremlin> g = TinkerGraph.open().traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.addV().property("p1", 1).property("p2", 10).iterate()
gremlin> g.addV().property("p1", 2).property("p2", 20).iterate()
gremlin> g.V().properties("p1", "p2").group().by(key).by(value().sum())
==>[p1:3,p2:30]
source to share