How to group results by vertex?

45 views
Skip to first unread message

Venkata Phani Kumar Mangipudi

unread,
May 20, 2016, 8:49:12 AM5/20/16
to Gremlin-users
Hi,

I've an organization graph where there are employees working in a group. I've properties at all the three graph elements (Group, Employee, and workingAt edge).

Employee (V)  - workingAt (E) -> Group (V)

Now I want to fetch all the employees working at the Group. Following was my attempt to retrieve the results:

gremlin> :> g.V().has('groupId','404a7920').inE('workingAt').outV().path()
==>[v[4168], e[5xj-37c-5445-37s][4152-worksAt->4168], v[4152]]
==>[v[4168], e[93d-6dk-5445-37s][8264-worksAt->4168], v[8264]]
==>[v[4168], e[f0p-9jc-5445-37s][12360-worksAt->4168], v[12360]]


I actually want to retrieve the grouped result per group:

==>[v[4168], [e[5xj-37c-5445-37s][4152-worksAt->4168], v[4152]], [e[93d-6dk-5445-37s][8264-worksAt->4168], v[8264]], [e[f0p-9jc-5445-37s][12360-worksAt->4168], v[12360]]]

I've tried multiple ways but couldn't get what I am actually wanting. Kindly help with this one.

Regards,
Phani

Daniel Kuppitz

unread,
May 20, 2016, 10:13:10 AM5/20/16
to gremli...@googlegroups.com
You already seem to have the group information (since you know its id) and your query only seems to target a single group, so why not just do this:

g.V().has('groupId','404a7920').inE('workingAt').as("w").outV().as("e").select("w","e")

Or if you really need to have the group vertex included:

g.V().has('groupId','404a7920').project("group","details").by().by(inE('workingAt').as("w").outV().as("e").select("w","e").fold())

Cheers,
Daniel



--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/e565d4a2-7341-4c60-88b3-34c37ab51150%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Venkata Phani Kumar Mangipudi

unread,
May 20, 2016, 10:01:59 PM5/20/16
to Gremlin-users
Thank you very much Daniel for your quick answer. I do need the group vertex as well, because all I've is the groupId, I would have to retrieve the other attributes of the group vertex as well. 

g.V().has('groupId','404a7920').project("group","details").by().by(inE('workingAt').as("w").outV().as("e").select("w","e").fold())

Unfortunately, the above query gives me an exception:

No signature of method: org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal.project() is applicable for argument types: (java.lang.String, java.lang.String) values: [group, details]
Possible solutions: reset(), collect(), toSet(), inspect(), sort(), inject(groovy.lang.Closure)
Display stack trace? [yN]

I am using Titan 1.1.0-SNAPSHOT built from titan11 branch.

Thanks and Regards,
Phani

Daniel Kuppitz

unread,
May 21, 2016, 5:29:17 AM5/21/16
to gremli...@googlegroups.com
g.V().has('groupId','404a7920').project("group","details").by().by(inE('workingAt').as("w").outV().as("e").select("w","e").fold())

... is the same as:

g.V().has('groupId','404a7920').as("group","details").select("group","details").by().by(inE('workingAt').as("w").outV().as("e").select("w","e").fold())

Cheers,
Daniel


Venkata Phani Kumar Mangipudi

unread,
May 22, 2016, 1:35:02 PM5/22/16
to Gremlin-users
Thank you very much Daniel,

This is very close to what I am actually looking for. Following was the output I've got

==>[group:v[4168], details:[[w:[v[4168], e[5xj-37c-5445-37s][4152-worksAt->4168]], e:v[4152]], [w:[v[4168], e[93d-6dk-5445-37s][8264-worksAt->4168]], e:v[8264]], [w:[v[4168], e[f0p-9jc-5445-37s][12360-worksAt->4168]], e:v[12360]]]]


I've gone through the Tinkerpop documentation for the Steps. But couldn't discern these tricky patterns in queries. Is there a documentation which I am missing where such combinations are explained? Kindly point me to a place where I could learn more about complex queries on Gremlin and pipes.

Thanks and Regards,
Phani

Daniel Kuppitz

unread,
May 22, 2016, 4:46:31 PM5/22/16
to gremli...@googlegroups.com
Hi Phani,

I don't think there's a general recipe for writing complex queries. Just get familiar with all the single steps and train yourself by trying to solve problems posted by other people on our mailing list.

Cheers,
Daniel


Venkata Phani Kumar Mangipudi

unread,
May 23, 2016, 1:13:18 AM5/23/16
to Gremlin-users
Sure Daniel, Thank you very much once again.

I'll practice as you advised.

Thanks and Regards,
Phani
Reply all
Reply to author
Forward
0 new messages