TinkerPop Gremlin Repeat until nodes with specified label

Viewed 147

In my graph there are 2 type of labels: a and b and a boolean property travel_by.

I would like to perform BFS (with max depth of 5): start from a given node and get all the first nodes with label a.

I tried to do something like this:

g.V(<node_to_start_from>).repeat(__.both().has("travel_by", True).simplePath())
.times(5)
.until(__.hasLabel('a')).toList()

But this query is stuck for a really long time (even if I change to times(2))

1 Answers

A common way to do this is to use loops

g.V(<node_to_start_from>).
  repeat(__.both().has("travel_by", True).simplePath()).
  until(__.hasLabel('a').or().loops().is(5)).
  hasLabel('a').
  toList()

The second has prevents anything other than items with a label of 'a' from being part of the result in cases where loops has reached 5.

Related