4

how to add an edge and dropping an edge to same vertex type in single gremlin query.

say we have two types of vertex types A --is--> B now i have vertex which is connected to some other vertex of B.

I want to update the A's vertex to some other vertex of B.

currently, i am dropping the current B vertex and then adding the new vertex .

stephen mallette
  • 45,298
  • 5
  • 67
  • 135
Uttkarsh Jain
  • 228
  • 1
  • 12

1 Answers1

6

You can do it in one traversal using a sideEffect():

gremlin> g.V().has('person','name','marko').as('m').
......1>   outE('knows').
......2>   filter(inV().has('person','name','vadas')).
......3>   sideEffect(drop()).
......4>   V().has('person','name','peter').
......5>   addE('knows').from('m')
==>e[13][1-knows->6]

At line 1 we basically identify the edge we want to get rid of (i.e. a "knows" edge from "marko" to "vadas") and we drop() that on line 3. At line 4 we lookup the vertex to whom we wish to connect "marko" to now and then add the edge at line 5.

stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • Hey @Stephen, Any idea how can this be done in gremlin-scala. sideeffect is not working as expected in gremlin-scala – Uttkarsh Jain Mar 06 '19 at 16:37
  • i am not able to use sideEffect as expected.g.V(businessVertex).as("a").outE() .and(_.label.is(EdgeLabels.IS_LABEL), _.inV().hasLabel(VertexLabels.TRIPTYPE_LABEL)).sideEffect(_=>__.drop()).iterate(). .. it not working its erroring out – Uttkarsh Jain Mar 07 '19 at 08:14
  • [InvalidQueryException: Could not read the traversal from the request sent. Cause: Could not locate method: DefaultGraphTraversal.sideEffect([{}])] – Uttkarsh Jain Mar 07 '19 at 18:37
  • why do you have `sideEffect()` being called that way (i.e. with what i think is a lambda)? should just be as i have it `sideEffect(_.drop())` – stephen mallette Mar 07 '19 at 18:56
  • It does not work without it. It throws compilation issues. – Uttkarsh Jain Mar 08 '19 at 03:26
  • I think you might need to raise an issue with gremlin-scala then maybe - https://github.com/mpollmeier/gremlin-scala/issues - that syntax works in all versions of Gremlin - http://tinkerpop.apache.org/javadocs/current/core/org/apache/tinkerpop/gremlin/process/traversal/dsl/graph/GraphTraversal.html#sideEffect-org.apache.tinkerpop.gremlin.process.traversal.Traversal- – stephen mallette Mar 08 '19 at 11:24
  • It should be `__.drop()`, not just `_.drop()`. – Daniel Kuppitz Mar 08 '19 at 15:05
  • i thought gremlin-scala repurposed the double underscore for just single underscore. guess i could be wrong about that – stephen mallette Mar 08 '19 at 15:24
  • @DanielKuppitz, could you please explain why we need a double score in gremlin scala. in DSE Studio we use double underscore. – Uttkarsh Jain Mar 11 '19 at 17:21
  • Oh, I thought you guys just had a copy/paste typo. Now I actually looked at the examples on https://github.com/mpollmeier/gremlin-scala and they use `_` as well as `__`, but I guess `_` is the right choice for scala. Sorry for the confusion. – Daniel Kuppitz Mar 11 '19 at 17:37