Sumit Chourasia
Sumit Chourasia

Reputation: 2434

How to get the sum of a edge field in graph database using gremlin?

I'm having two vertices Named - UserName and CompanyName, Whenever user visits any company I increases edge field (i.e pageVisit). for individual results i'm getting those pageVisit values but How can i get the Sum of all user pageVisits?

I want to show total Page Visits on a company page, I didn't find any way to add the edge values.

Upvotes: 0

Views: 621

Answers (1)

stephen mallette
stephen mallette

Reputation: 46226

You can use the sum() step:

gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> vUser1 = graph.addVertex("UserName","user1")
==>v[0]
gremlin> vUser2 = graph.addVertex("UserName","user2")
==>v[2]
gremlin> vCompany = graph.addVertex("CompanyName","Company")
==>v[4]
gremlin> vUser1.addEdge('visits',vCompany,'pageVisit',10)
==>e[6][0-visits->4]
gremlin> vUser2.addEdge('visits',vCompany,'pageVisit',20)
==>e[7][2-visits->4]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:3 edges:2], standard]
gremlin> g.V(vCompany).inE('visits').values('pageVisit').sum()
==>30

Upvotes: 3

Related Questions