Recursively get vertices of nested groups

82 views
Skip to first unread message

Sean McCarthy

unread,
Sep 6, 2023, 2:30:44 PM9/6/23
to Gremlin-users
I am a bit new to gremlin and struggling with the query to get results I want if someone has any idea. This may be an atypical use cause but it's what I currently need.

I have a DAG which will consist of two things
Groups
Elements

Groups have edges to both other groups and elements
(Again it's a DAG, so we are not worried about circularity)
Elements have no outward edges

I am looking for a query starting from given vertex, g.V("groupid") that will traverse every possible outward edge including children of that vertex and their vertices and get the list of element vertices id's. Basically trying to do recursive tree search except always searching the entire tree.

I tried something like this, 
g.V().hasId('GroupVertexID').repeat(__.out()).until(__.hasLabel('Element')).toList()
From what I can tell this only has a depth of 1 though.
Any help is appreciated!

Cole Greer

unread,
Sep 6, 2023, 4:11:14 PM9/6/23
to gremli...@googlegroups.com
Hi Sean,

Your traversal looks correct to me based on what you are trying to do, it is very similar to this example from the reference docs:
gremlin> g.V(1).repeat(out()).until(hasLabel('software')).path().by('name') //// (1)
==>[marko,lop]
==>[marko,josh,ripple]
==>[marko,josh,lop]
I would suggest double checking your graph to ensure there are multiple 'Element' leaf nodes in your 'GroupVertexID' subtree. If something still looks wrong let me know and I can load up a sample graph based on your description and try it out.

Regards,

Cole Greer

--
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/5b94d74b-3fac-4d1e-8701-744aff1b7651n%40googlegroups.com.

Sean McCarthy

unread,
Sep 6, 2023, 5:35:45 PM9/6/23
to Gremlin-users
Thanks Cole,
Indeed I must have missed something in my original testing.
Tested with a simple but representative graph like suggested and it works!

g.addV('Group').as('1').addV('Group').as('2').addV('Element').as('3').addV('Element').as('4').
addV('Element').as('5').addV('Group').as('6').addV('Element').as('7').addE('child').from('1').to('3').addE('child').
from('1').to('2').addE('child').from('1').to('4').addE('child').from('2').to('4').addE('child').from('2').to('5').addE('child').
from('1').to('6').addE('child').from('6').to('7')
Screen Shot 2023-09-06 at 5.32.16 PM.png

Best,
Sean McCarthy
Reply all
Reply to author
Forward
0 new messages