Search code examples
gremlintinkerpop

Condensing Gremlin queries into one


I have two queries that delete certain vertices in a graph for the same initial vertex

g.V(id).outV().drop().iterate()
g.V(id).drop().iterate() 

Is it possible to combine these two queries into one?

Second question is how can perform some terminal operation on vertices before they are dropped, I tried with sideEffect, but it needs to return value

g.V(id).outV().sideEffect(outV().forEachRemainig(x -> // do something)).drop()

Solution

  • For your initial question you can accomplish this via a sideEffect() like this:

    g.V(id).sideEffect(out().drop()).drop()
    

    For the second traversal you can accomplish this by switching the sideEffect() to performing the drop and then put the remaining operations to be part of the main traversal stream. Since sideEffect() streams the incoming traversals to the output you will be able to perform operations on them like this:

    g.V(id).sideEffect(drop()).valueMap()
    

    Just a note here, in your original traversals you went g.V(id).outV() which is not allowed as outV() only works from an edge, so I changed it to out() which takes you to the adjacent vertex.