I'm new to Gremlin and just trying to build out a basic graph. I've been able to do a basic addEdge on new vertices, i.e.
gremlin> v1 = g.addVertex()
==>v[200004]
gremlin> v2 = g.addVertex()
==>v[200008]
gremlin> e = g.addEdge(v1, v2, 'edge label')
==>e[4c9f-Q1S-2F0LaTPQN8][200004-edge label->200008]
I have also been able to create an edge between vertices looked up by id:
gremlin> v1 = g.v(200004)
==>v[200004]
gremlin> v2 = g.v(200008)
==>v[200008]
gremlin> e = g.addEdge(v1, v2, 'edge label')
==>e[4c9f-Q1S-2F0LaTPQN8][200004-edge label->200008]
However, I now want to look up vertices based on multiple properties, which is where it gets tricky. In order to look up the right vertex, I'm making 2 calls to .has
. It appears that the correct vertices are found, but adding the edge fails.
gremlin> v1 = g.V.has("x",5).has('y",7)
==>v[200004]
gremlin> v2 = g.V.has("x",3).has('y",5)
==>v[200008]
gremlin> e = g.addEdge(v1, v2, 'edge label')
No signature of method: groovy.lang.MissingMethodException.addEdge() is applicable for argument types: () values: []
What's the easiest way to add a simple edge between two existing vertices, based on a property value lookup?
The key issue is that .has
returns a Pipe: in order to get the specific vertex instance, a simple call to .next()
does the trick:
gremlin> v1 = g.V.has("x",5).has('y",7).next()
==>v[200004]
gremlin> v2 = g.V.has("x",3).has('y",5).next()
==>v[200008]
gremlin> e = g.addEdge(v1, v2, 'edge label')
==>e[4c9f-Q1S-2F0LaTPQN8][200004-edge label->200008]
Note that .next()
will simply return the next item in the pipe. In this case, any additional vertices matching the property values are ignored.