Search code examples
gremlinjanusgraphgremlin-server

Gremlin - if multiple vertices return multiple values each, how to limit the result to one per vertex


Essentially, I'm trying to modify the following piece of Gremlin code such that instead of operating on a single vertex at a time - signified by g.V(1), it will work with multiple vertices at once (e.g. changing to g.V()), while still only limiting the number of returned results per vertex to one (see limit(1)).

g.V(1).repeat(out().simplePath()).until(has('color', 'red')).path().limit(1)

The above query will compute the shortest path from a given vertex to the closest vertex which has property(color)==red.

However, I want to compute the shortest path for multiple vertices passed in at the same time, while still only returning a single path per vertex.

I'm having difficulty modifying this without returning multiple paths for the same vertex however.


Solution

  • Deduplicating the result by the start vertex should give you the expected result.

    g.V().as('a').
      repeat(out().simplePath()).
        until(has('color', 'red')).
      dedup('a').
      path()
    

    Example using the modern toy graph:

    gremlin> g = TinkerFactory.createModern().traversal()
    ==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
    gremlin> g.V().
    ......1>   repeat(out().simplePath()).
    ......2>     until(hasLabel('software')).
    ......3>   path()
    ==>[v[1],v[3]]
    ==>[v[1],v[4],v[5]]
    ==>[v[1],v[4],v[3]]
    ==>[v[4],v[5]]
    ==>[v[4],v[3]]
    ==>[v[6],v[3]]
    gremlin> g.V().as('a').
    ......1>   repeat(out().simplePath()).
    ......2>     until(hasLabel('software')).
    ......3>   dedup('a').path()
    ==>[v[1],v[3]]
    ==>[v[4],v[5]]
    ==>[v[6],v[3]]