Search code examples
gremlintinkerpopgremlin-server

Gremlin: get a list of child nodes of a certain label n-levels deep


i'm struggling to figure out the correct command here.

Starting from a given node I would like to get all child nodes of a certain label n-levels deep

e.g.

imagine we start at a node with label 'A'.

A has child nodes of type B or C (1 level deep)

These child nodes B or C can have children of type D or E (2 levels deep)

These child nodes can again have further child nodes of the same type D or E and so on (3+ levels deep)

I would like to extract all nodes of type E that are 2 levels deep from A i.e., not look at their children that are 3+ levels deep that could also be of type E

Thanks for any help!

I guess the starting part of the command would be g.V('A')

Edit: See Kelvin's solution - that did the trick for me.


Solution

  • In its simplest form, finding all nodes, 2 hops away from a given start, that have a specific label is as simple as:

    g.V('A').repeat(out()).times(2).hasLabel('E')