Search code examples
gremlingraph-databasesgremlin-serverazure-cosmosdb-gremlinapi

Gremlin query to traverse graph bottom to top capturing vertex children


We use Gremlin 3.2 compatible database. Actually, we try Cosmos DB in graph/gremlin mode. But I am looking to gremlin query so not necessary it is related only to Cosmos DB

The graph looks like on the image below enter image description here

I have created a query that is able to capture red vertices/nodes.

g.V("A").emit().repeat(__.in('depends')).until(__.inE().count().is(0))

But struggling to extend a query to capture direct children of each vertex. On the image marked as blue ones.

Can anybody help with such a gremlin query?

Here is a query in online editor https://gremlify.com/spml2ktk03 If using the online editor:

  1. Actual: B->A->D->TOP
  2. Expected: B-> A (including info about C) -> D (including info about E) -> TOP

Solution

  • You should be able to accomplish what you are looking for by using store() to store all the vertices in the tree and then for each iteration of the repeat you can use sideEffect() to find it's children and store them as well.

    g.V().hasLabel('B').store('v').
      repeat(__.in('depends').store('v').
        sideEffect(out('depends').store('v'))).
      until(__.inE().
        count().is(0)).cap('v')
    

    This returns me:

    B -> A -> C- > D -> E -> TOP