Reputation: 474
I want to remove a set of vertices together with all its descendants along given edge relations. In my case, the graph has some vertices whose type is session, and others whose type is event. There are arcs from events to sessions with a label in_session.
It seems there is no quick way to do this in one pass. Here is the code I used:
// First, select all event descendants of sessions with r < 700
GraphTraversal<Vertex, Vertex> t = g.V()
.has("type", "session")
.has("r", P.lt(700))
.in("in_session");
// And remove those events
while (t.hasNext())
t.next().remove();
// Then, using the first part of the query again,
// select the same sessions as before
t = g.V().has("type", "session").has("r", P.lt(700));
// And delete them as well
while (t.hasNext())
t.next().remove();
It seems very clunky to me. Moreover, if I wanted to remove lower level descendants I'd have to write more repeated steps (all the way down to the bottom, then remove, then back up one level, then remove, then back up one level again and so on...). Also I noticed there is no removeVertex method in TitanGraph.
Upvotes: 0
Views: 61
Reputation: 46226
One approach would be to use sideEffect()
. Let's say I want to remove all "knows" vertices from the "marko" vertex in the TinkerPop toy "modern" graph:
gremlin> graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().has('name','marko').outE()
==>e[9][1-created->3]
==>e[7][1-knows->2]
==>e[8][1-knows->4]
gremlin> g.V().has('name','marko').sideEffect(out('knows').drop()).drop()
gremlin> g.V().has('name','marko')
gremlin> g.V(2)
gremlin> g.V(4)
gremlin> g.V(3)
==>v[3]
Note the use of drop()
as opposed to iterating the traversal to call remove()
on each element.
Upvotes: 2