Gremlin Query to fetch all objects till specified level along with sibling nodes

57 views Asked by At

I have a Gremlin hierarchy of nodes which are referenced (Refer below pic). How can I retrieve all nodes, including their sibling nodes, at a specific provided level from root node ?

enter image description here

For instance,

  1. If I provide level 1 as input, I want Eq1, Eq2, Eq3, and Eq5 as an output.
  2. If I provide level 2 as input, I want Eq1, Eq2, Eq3, Eq4, and Eq5 as an output
  3. If I provide level 3 as input, I want Eq1, Eq2, Eq3, Eq4, Eq4.1, and Eq5 as an output.

    Note: Output nodes can be in any sequence.

How can I achieve this using Gremlin tinkerpop from root node ?

1

There are 1 answers

0
dragosmc On

You could use the repeat step in gremlin and go out the adjacent nodes until you match your desired level.

For the first case you can do something like this, without repeat, as you only need the adjacent nodes.

g.V('root_node').out().toList()

For the rest of the use cases you can use the repeat like so:

g.V('root_node').repeat(out()).times(2).path() # For level 2

and

g.V('root_node').repeat(out()).times(3).path() # For level 3

Lastly, here's the docs for the repeat() step https://tinkerpop.apache.org/docs/current/reference/#repeat-step

Note: the queries are not tested on a gremlin server but you should be able to tweak them for your use case/