recursive query with hierarchical result

1,012 views
Skip to first unread message

Greg H

unread,
Jun 8, 2016, 7:57:57 PM6/8/16
to Gremlin-users
I have a graph structure representing a hierarchy of arbitrary depth, ie  

V -child-> V -child-> V -child-> V -child-> V ... 

I want to query a subset of the graph rooted at some V traversing all child edges recursively to some depth d, returning all found vertices and their properties in (ideally) a hierarchical structure. 

So for example given the graph

V(a) -child-> V(b)
V(a) -child-> V(c)
V(b) -child-> V(d)
V(c) -child-> V(e)

the query for the graph rooted at v(a) to depth 2 would look like

{ id:'a',
  keys:values,
  child: [{
    id:'b',
    keys:values,
    child:[{
     id:'d'
     keys:values
    },
    {
    id:'c',
    keys:values,
    child:[{
      id:'e',
      keys:values
    }]
  }]
}]

I'm new to gremlin and wondering if this is doable and how? 
Message has been deleted

Daniel Kuppitz

unread,
Jun 8, 2016, 8:07:12 PM6/8/16
to gremli...@googlegroups.com
If you're using TP2, use the linked Wiki page. But if you're using TP3:

g.V("some V").repeat(out("child").simplePath().dedup()).times(depth).tree().next()

Cheers,
Daniel


On Thu, Jun 9, 2016 at 2:01 AM, Greg H <gr...@itactual.com> wrote:
Nevermind - https://github.com/tinkerpop/gremlin/wiki/Tree-Pattern seems to fit the bill nicely 

--
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/94c72d38-c45f-4024-bfe4-4e4515c3c4a8%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Greg H

unread,
Jun 9, 2016, 9:18:27 AM6/9/16
to Gremlin-users
Thanks Daniel.

I'm on the TP2 stack and a simple tree() pattern query almost works, eg

g.V('x').out('child').out('child').tree().cap()  

This returns a subset of the graph rooted at x with a depth of 2, but it excludes paths with a depth less than 2 (ie child node without children) and I need to include those as well. To rephrase the requirement I want to select the subset of the tree rooted at x vertexes related by child to a depth up to and including d. 

Any suggestions on how to format that query in TP2? :)  

Cheers,
Greg

Marko Rodriguez

unread,
Jun 9, 2016, 9:23:05 AM6/9/16
to gremli...@googlegroups.com
Hi Greg,

If you want both 0-step, 1-step, 2-step, etc. branches in your tree, simply use repeat() and emit().

gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().repeat(out()).times(2).emit().tree()
==>[v[1]:[v[2]:[:], v[3]:[:], v[4]:[v[3]:[:], v[5]:[:]]], v[4]:[v[3]:[:], v[5]:[:]], v[6]:[v[3]:[:]]]
gremlin> g.V().repeat(out()).times(2).emit().tree().next()
==>v[1]={v[2]={}, v[3]={}, v[4]={v[3]={}, v[5]={}}}
==>v[4]={v[3]={}, v[5]={}}
==>v[6]={v[3]={}}
gremlin>

The times(2) is the upper limit and emit() says, “at every iteration of the walk, yield what has been touched thus far.

HTH,
Marko.

Daniel Kuppitz

unread,
Jun 9, 2016, 10:50:06 AM6/9/16
to gremli...@googlegroups.com
This should be the equivalent TP2 query:

g.v("some V").as("x").out("child").simplePath().dedup().loop("x") {it.loops <= depth} {true}.tree().cap()

Cheers,
Daniel


Greg H

unread,
Jun 9, 2016, 8:36:05 PM6/9/16
to Gremlin-users
Vero cool.  Still learning this stuff, thanks Marko

Greg H

unread,
Jun 9, 2016, 8:36:25 PM6/9/16
to Gremlin-users
Awesome thx. 

I'm in the process of upgrading 

Greg H

unread,
Jun 10, 2016, 5:31:29 PM6/10/16
to Gremlin-users
Mark,

Thanks for the help. 

The second form of the query is easier for me to process using a node client, so:

gremlin>g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V(1).repeat(out()).times(2).emit().tree().next()
==>v[1]={v[2]={}, v[3]={}, v[4]={v[3]={}, v[5]={}}}
gremlin> g.V(4).repeat(out()).times(2).emit().tree().next()
==>v[4]={v[3]={}, v[5]={}}
gremlin> g.V(3).repeat(out()).times(2).emit().tree().next()
gremlin> 

The V(1) and V(4) results for this query are exactly what I want - the query root and the set of out traversals to depth of 2 inclusive - but the 0 step case returns an empty result. Would it be straightforward to return the query root and an empty traversal set, ie v[3]={} for this case? 

Thx again. 
  


On Thursday, June 9, 2016 at 7:23:05 AM UTC-6, Marko A. Rodriguez wrote:

Daniel Kuppitz

unread,
Jun 10, 2016, 5:57:00 PM6/10/16
to gremli...@googlegroups.com
If the emit() comes before repeat(), elements will be emitted prior to the repeat() traversal being executed.

gremlin> g.V(1).emit().repeat(out()).times(2).tree().next()

==>v[1]={v[2]={}, v[3]={}, v[4]={v[3]={}, v[5]={}}}
gremlin> g.V(4).emit().repeat(out()).times(2).tree().next()

==>v[4]={v[3]={}, v[5]={}}
gremlin> g.V(3).emit().repeat(out()).times(2).tree().next()
==>v[3]={}

Cheers,
Daniel


Greg H

unread,
Jun 10, 2016, 7:51:47 PM6/10/16
to Gremlin-users
Thats got it - tq Daniel
Reply all
Reply to author
Forward
0 new messages