Search code examples
graphgremlintinkerpopedgesdatastax-enterprise-graph

Gremlin: adding edges between nodes having the same property



I am very new to Gremlin. I am trying to build a graph on DSE graph using Gremlin. I am able to create the vertices:

a = graph.addVertex(label, 'label1', 'key', 1)
b = graph.addVertex(label, 'label1', 'key', 2)
c = graph.addVertex(label, 'label2', 'key', 1)
d = graph.addVertex(label, 'label2', 'key', 2)

Now i am looking to automatically add edges between two nodes with differents label where the property 'key' matches (i.e create and edge between a and c, and between b and c). I am stuggling to do that.

I tried to do the following

 g.V().hasLabel("label1").sideEffect{g.V().("label2").has("key",it.key).addEdge("link",it)}

But I am getting the following error:

No signature of method: org.apache.tinkerpop.gremlin.process.traversal.traverser.B_O_Traverser.values() is applicable for argument types: (java.lang.String) values: [key]

Can somebody assists me on this issue ? Thank you by advance


Solution

  • Nested g.V()'s are usually a bad idea. You can solve the problem using a single traversal:

    g.V().hasLabel("label1").as("a").
      V().hasLabel("label2").as("b").
      where("a", eq("b")).by("key").
      addE("link").from("a").to("b")
    

    Also note that you'll have to allow scans in DSE Graph to make this traversal work.