0
votes

Let's say I have this graph (link to Gremlify):

Graph with three nodes, two of which have a relationship

I need a query to get a vertex with all its child vertices (up to a certain depth). We already have one that seems to work:

g.V("4840")
 .repeat(outE().inV().simplePath())
 .times(5)
 .emit()
 .path()
 .by(valueMap(true))

The result is:

[
  [
    {
      "id": 4840,
      "label": "Vertex"
    },
    {
      "id": 4842,
      "label": "child"
    },
    {
      "id": 4838,
      "label": "Vertex"
    }
  ]
]

So I now have the vertex with the edge and the child vertex. Great. But what if I use id 4836? Then I get an empty array. This is because this vertex doesn't have any outgoing edges. Is there a way to construct a query that will give me a vertex with its linked vertices, but also the vertex itself if it doesn't have any edges?

You can play around with this here.

1

1 Answers

1
votes

You just need to switch the repeat() from while..do to do..while by moving the emit() to the front of the repeat():

g.V("4840")
 .emit()
 .repeat(outE().inV().simplePath())
 .times(5)
 .path()
 .by(elementMap())