11

I want to remove edge between two vertices, so my code in java tinkerpop3 as below

private void removeEdgeOfTwoVertices(Vertex fromV, Vertex toV,String edgeLabel,GraphTraversalSource g){
        if(g.V(toV).inE(edgeLabel).bothV().hasId(fromV.id()).hasNext()){
            List<Edge> edgeList = g.V(toV).inE(edgeLabel).toList();
            for (Edge edge:edgeList){
                if(edge.outVertex().id().equals(fromV.id())) {
                    TitanGraph().tx();
                    edge.remove();                    
                    TitanGraph().tx().commit();
                    return;//Remove edge ok, now return.
                }
            }
        }
    }

Is there a simpler way to remove edge between two vertices by a direct query to that edge and remove it? Thank for your help.

MichaelP
  • 2,691
  • 3
  • 29
  • 35

1 Answers1

19

Here's an example of how to drop edges between two vertices (where you just have the ids of those vertices:

gremlin> graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V(1).bothE()
==>e[9][1-created->3]
==>e[7][1-knows->2]
==>e[8][1-knows->4]

For purpose of the example, let's say we want to drop edges between vertex 1 and vertex 2. We could find those with:

gremlin> g.V(1).bothE().where(otherV().hasId(2))
==>e[7][1-knows->2]

and then remove it with:

gremlin> g.V(1).bothE().where(otherV().hasId(2)).drop()
gremlin> g.V(1).bothE()
==>e[9][1-created->3]
==>e[8][1-knows->4]

If you have the actual vertices, then you could just do:

gremlin> g.V(v1).bothE().where(otherV().is(v2)).drop()
gremlin> g.V(1).bothE()
==>e[9][1-created->3]
==>e[8][1-knows->4]

You could re-write your function as:

private void removeEdgeOfTwoVertices(Vertex fromV, Vertex toV,String edgeLabel,GraphTraversalSource g){
    g.V(fromV).bothE().hasLabel(edgeLabel).where(__.otherV().is(toV)).drop().iterate();
    g.tx().commit();    
}
stephen mallette
  • 39,757
  • 4
  • 49
  • 112
  • Thank you. This is very helpful for me. Tinkerpop3 is new and not too many examples. If possible, can you give me more resources,links or references where i can find more examples? I need to learn more gremlin from examples to optimize code. Thank you again. – MichaelP Jan 05 '16 at 01:41
  • 1
    More documentation is under development, but as of right now you only have the reference documentation where there are examples of nearly all the available steps. – stephen mallette Jan 05 '16 at 11:11
  • it works when i change to g.V(fromV).bothE(edgeLabel).where(__.otherV().is(toV)).drop().iterate(); g.tx().commit(); – MichaelP Jan 08 '16 at 07:52
  • I guess drop() doesn't auto-iterate - i thought it was a terminating step - sorry about that. – stephen mallette Jan 08 '16 at 16:34
  • Exactly, the problem in this language is that things do not happen immediately. Up to `drop()` is just a `pipeline` that it is executed only when you call `iterate()` or `next()` They show it in the console, which automatically consumes the command, so is like there is an hidden step in all their instructions – Kuzeko Feb 04 '17 at 14:43
  • I would like to hide the edge relation instead of removing or destroying the connection so that I can retrieve the relation at a later stage. Could you please explain how to do this? Thanks. – Shr4N Jun 25 '19 at 11:09
  • "hide"? i guess you just mean you would add a property of "hidden" to the edge? if so, instead of `drop()` you would just do `property('hiddent',true)`. not sure if that answers your question. if not, perhaps you should create a new question here on SO. – stephen mallette Jun 25 '19 at 11:55