Home > Software engineering >  Gremlin - find subgraph from specific starting points
Gremlin - find subgraph from specific starting points

Time:12-12

I am having problems with creating quite a simple query:

Lets assume a graph with nodes representing people and edges of type 'has child'.

I get a subset of the people as starting point and I need to find all the descendants (recursively). More specifically, I am only interested in the edges:

Example:

a -> b -> c
d -> b
(starting points = [c]) => []
(starting points = [b]) => [b->c]
(starting points = [a,b,c]) => [a->b, b->c]
(starting points = [d]) => [d->b, b->c]

So far, I got to this query:

g.V().has('name', 'something'). // this line gets replaced by various filters
repeat(outE('child').dedup().inV()).
until(
    outE('child').count().is(eq(0))
).
path(). // get all paths
unfold(). // flatten the list of lists
filter(hasLabel('child')). // filter out only edges
dedup()

However this query does not work properly, if we select more starting points on the same path (an example can be selecting all vertices by doing g.V()....)

CodePudding user response:

Assuming you want to get back a deduplicated list of all the edges crossed between the start vertices until the leaf nodes are reached, a query like this will work:

g.V('start-id').
  repeat(outE('child').store('edges').inV()).
  until(not(out('child'))).
  cap('edges').
  unfold().
  dedup()
  • Related