Search code examples
gremlintinkerpoptinkerpop3

Finding all path with specific edge types in Tinkerpop 3


I want to find all path available with "friend" edge:

g.v(1).out("friend").out("friend").....

so the result will be all paths from v(1) which can be connected by this edge type. it would be great if I also can limit the search length and can have multiple edge type instead of "friend", something like:

g.v(1).out("friend" or "enemy").out("friend" or "enemy). .....

what would be gremlin way if I have two nodes and want to find all path/shortest path?


Solution

  • Use jump to recursively traverse down a path and the path step to realize the path traversed:

    gremlin> g = TinkerFactory.createClassic()
    ==>tinkergraph[vertices:6 edges:6]
    gremlin> g.V(1).as('x').out('knows').jump('x'){it.loops<3}{true}
    ==>v[2]
    ==>v[4]
    gremlin> g.V(1).as('x').out().jump('x'){it.get().id() != 5 && it.loops < 6}.path()
    ==>[v[1], v[4], v[5]]
    

    See more details in GremlinDocs: