5

I have a requirement to drop all child nodes when parent node is dropping using gremlin query.

suppose

                               A
                             /   \
                            B     C
                           /  \     
                          D    E 
                        /   \
                       F     G
                            /  \
                           H    I

If we want to drop 'B' vertex then its childs 'D','E','F','G','H' &'I'vertex also should be drop. Could you please tell me how to drop.

cva
  • 61
  • 1
  • 5

1 Answers1

8

I assume that "B" is an indexed property called "myId":

g.V().has('myId','B').
  union(__(),
        repeat(out()).emit()).
  drop()

The use of union() does two things as in this case it has two arguments. The first is the __(). This is just an identity function. It includes the "B" vertex in the result of dropped vertices. In other words, the union() receives the "B" vertex as it's input, and we do nothing with it except include it in the results of the union(). The second argument to the union takes the "B" vertex and traverses out recursively to find all the child vertices. The use of emit() here is important because it tells the repeat to return all the child vertices it finds along the way (and not just the leaves of the tree).

In the comments it was mentioned that __() is not supported by Microsoft's version of Gremlin in their .NET client. Hopefully they are working on that. I guess you can work around that with this bit of ugliness (or something similar i suppose):

g.V().has('myId','B').
  union(fold().unfold(),
        repeat(out()).emit()).
  drop()

Another approach might be to use store():

g.V().has('myId','B').
  store('d').
  repeat(out().store('d')).
  cap('d').
  unfold().
  drop()

The store approach isn't as nice as the previous approach because it has to build a List in memory that you extract from the "d" side-effect with cap() and then unfold that list back to the iterator to be dropped.

stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • Hi stephen,Thank you for your replay, I am new to the gremlin ,what does __ mean in union method , i have got an exeption "The name `__' does not exist in the current context".Can you please tell – cva Jul 21 '17 at 20:03
  • Thanks for your description,I have tried like "g.V().has('id','thomas1').union(g.V('thomas1'),repeat(out()).emit().drop()" because i am using through .net there identity function inside union through exception .So i have tried like above But its only dropped the thomas1 edge not the thomas1 childs. – cva Jul 25 '17 at 07:09
  • That's too bad about `__()`. I'm a bit surprised that isn't supported. It's the easiest step to implement and while it is essentially just a pass-through function, you can see where it might have its uses. Anyway, updated my answer...hopefully one of those two additional approaches will work for you. – stephen mallette Jul 25 '17 at 10:37
  • I really appriciate you ,the way you are responding Thank you very much stephen. I do try your updated queries. – cva Jul 25 '17 at 12:23
  • 1
    I just came across this and gave it a go and just get this error: Vertex with id 12345 was removed. – default_avatar Jun 03 '20 at 12:36