4

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

Dzmitry Lahoda
  • 939
  • 1
  • 13
  • 34
Suezieq
  • 43
  • 2
  • 4
  • Perhaps you could amend your answer to include a sample graph using a Gremlin script as shown here in this answer: https://stackoverflow.com/a/47042021/1831717 - that might make this a bit easier to follow. – stephen mallette Nov 22 '17 at 21:12
  • Yes, sorry. Added to the main post. – Suezieq Nov 23 '17 at 00:23

1 Answers1

5

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]
stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • That's awesome Stephen! Much appreciated thank you! Happy Holidays. – Suezieq Nov 24 '17 at 13:45
  • Sorry again, I spoke too soon here. I tried that query and it seems it counts the total of the edges and total of the out nodes but not really grouping by each edge label. Example this post has 4 reviews, 2 to company and 2 to product. This post also has 4 rates, 2 to company and 2 to product. Ideally, the result of the query would be post|reviews:2|company:2, post|reviews:2|product:2, post|rates:2|company:2, post|rates:2|company:2. Is this even possible? – Suezieq Nov 24 '17 at 14:36
  • Updated gremlinbin with the additional edge types: http://gremlinbin.com/bin/view/5a18346c91c32. I even tried something like this: g.V(1).project('post','outEdges','outNodes').by(id).by(outE().groupCount().by(label)).by(out().groupCount().by(label)) to flatten it out but not working. – Suezieq Nov 24 '17 at 15:13
  • I don't understand the output that you presented there. why are the outputs in groupings of 2? perhaps the sample data you provided is confusing me in the sense that there are two of each type of thing so the numbers are all equal....not sure. I added another example that made sense to me given your original question but I may still not be getting it. – stephen mallette Nov 24 '17 at 19:11
  • Right. I tried that query too. What it does not answer is what are the edges to both product and company. I see post is related to company but through what edge - you know what I mean. Like path but grouped by edge, otherV – Suezieq Nov 24 '17 at 19:59
  • Here is an image that I would like to display https://pasteboard.co/GVaZwIJ.png The numbers signify the node group count for that edge – Suezieq Nov 24 '17 at 20:21
  • Does the edit i just made to my answer get you any closer to what you want? "grouped by edge, otherV" is what i tried to present there. – stephen mallette Nov 24 '17 at 20:39
  • OMG Perfect!!! :-) Thank you so much! Curious, is this a common query? – Suezieq Nov 24 '17 at 21:22
  • I ask because I search high and low and nothing I came across really captured the intention correctly. – Suezieq Nov 24 '17 at 22:05
  • Well, I'm not sure that this is an especially uncommon query. When you consider the series of steps in use and examine how they are being used, there's not a whole lot of "special" happening there. Perhaps the challenge is knowing the individual steps well enough to string them together in such a way as to produce the output you are looking for. That kind of knowledge just takes time and practice to ascertain. Glad I could help. – stephen mallette Nov 25 '17 at 00:57
  • Yes, time and practice indeed. I too agree, not magic there, but you are right, the skill is in knowing the correct structure which, is a bit elusive to us newbies. Maybe add that use case to the gremlin docs? Anyways, appreciate the help! Cheers! – Suezieq Nov 25 '17 at 01:27