4
votes

Use case: Get all the edges of a node, grouped by the label and also group the otherV by the label, display name, and counts for both.

Graph has the following nodes: post, product, company. post has outE named review which goes to both product and company.

For a postid, is there a way to get the count of reviews grouped by product and company.

I would like to display in a table the following info.

postid | review (count) | product (count) postid | review (count) | company (count)

I tried outE(), out() with groupby label but I cannot seem to construct a query that will give me the counts with multiple groupby's.

Any help would be most appreciated.
Thanks in advance.

graph = TinkerGraph.open()
g = graph.traversal()
v1 = graph.addVertex(id, 1, label, "post")
v2 = graph.addVertex(id, 2, label, "company")
v3 = graph.addVertex(id, 3, label, "company")
v4 = graph.addVertex(id, 4, label, "product")
v5 = graph.addVertex(id, 5, label, "product")
v1.addEdge("reviews", v2)
v1.addEdge("reviews", v3)
v1.addEdge("reviews", v4)
v1.addEdge("reviews", v5)

Image

1
Perhaps you could amend your answer to include a sample graph using a Gremlin script as shown here in this answer: stackoverflow.com/a/47042021/1831717 - that might make this a bit easier to follow. - stephen mallette
Yes, sorry. Added to the main post. - Suezieq

1 Answers

5
votes

I don't have the exact structure of what you asked for in your output, but perhaps this is close enough for your purposes as it does return the data that I believe that you asked for:

gremlin> g.V().hasLabel('post').
......1>   project('postid','reviews','types').
......2>     by(id).
......3>     by(outE().count()).
......4>     by(out().groupCount().by(label))
==>[postid:1,reviews:4,types:[product:2,company:2]]

Another option:

gremlin> g.V().hasLabel('post').
......1>   outE().as('e').
......2>   inV().as('i').
......3>   select('e','i').by(label).
......4>   groupCount().by(select('e','i'))
==>[[e:rates,i:company]:2,[e:reviews,i:company]:2,[e:reviews,i:product]:2,[e:rates,i:product]:2]