Trying to find a Vertex using a variable injected earlier in the traversal

I am trying to add a series of vertices and edges to an existing graph. The newly created Vertex will be the to Vertex. From business logic, I will know the ID of the from Vertex for the new Edge . I am able to create the new Vertex without any issue, but when I am having trouble grabbing the from Vertex to create the edge. Is there a way to do this/what am I doing wrong?
g.inject(["myID": "2", "parentID": "1", "properties": ["key1":"value1"]]).
addV("MyVertex").as("newVertex").
property(id, select("myID")).
sideEffect(
select("properties").unfold().as("kv")
select("newVertex").
property(
select("kv").by(keys),
select("kv").by(values),
)
).
AddE("MyEdge").to("newVertex").from(V(select("parentID")))
g.inject(["myID": "2", "parentID": "1", "properties": ["key1":"value1"]]).
addV("MyVertex").as("newVertex").
property(id, select("myID")).
sideEffect(
select("properties").unfold().as("kv")
select("newVertex").
property(
select("kv").by(keys),
select("kv").by(values),
)
).
AddE("MyEdge").to("newVertex").from(V(select("parentID")))
4 Replies
spmallette
spmallette14mo ago
This pattern is hard to implement for situations where you need to dynamically use the values in the Map to look-up an existing Vertex. While there is probably a way to make it work, this pattern is a bit antiquated given the introduction of mergeV() and mergeE() which generally simplifies that query to something more like:
gremlin> g.mergeV([(T.id): "2", key1: 'value1']).
......1> mergeE([(T.label): "MyEdge", (to): "2", (from): "1"])
==>e[1][1-MyEdge->2]
gremlin> g.mergeV([(T.id): "2", key1: 'value1']).
......1> mergeE([(T.label): "MyEdge", (to): "2", (from): "1"])
==>e[1][1-MyEdge->2]
Is there any chance you could use this approach instead or are you using a version of Gremlin prior to 3.6.0?
duffrey
duffrey14mo ago
Yeah, I can do that. I am on 3.6.2. I think the reason that I wanted to use the add functions instead of the merge function is that I wanted it to fail if there was a collision. Do you know if there is a way to set the OnMatch to throw an error? In this case I could do an addV and then know that the edge doesn't exist so the mergeE could only be a create. But if the merge functions are going to be more broadly capable it would still be good to know if there is a way to detect a collision from the merge
spmallette
spmallette14mo ago
you can use fail() step - the example from the docs shows its usage with onCreate but you could just as easily apply it to onMatch:
gremlin> g.mergeV([(T.id): 1]).
......1> option(onCreate, fail("vertex did not exist")).
......2> option(onMatch, [modified: 2022])
fail() Step Triggered
======================================================================================================================================================================
Message > vertex did not exist
Traverser> false
Bulk > 1
Traversal> fail("vertex did not exist")
Parent > TinkerMergeVertexStep [mergeV([(T.id):((int) 1)]).option(Merge.onCreate,__.fail("vertex did not exist")).option(Merge.onMatch,[("modified"):((int) 2022)])]
Metadata > {}
======================================================================================================================================================================
gremlin> g.mergeV([(T.id): 1]).
......1> option(onCreate, fail("vertex did not exist")).
......2> option(onMatch, [modified: 2022])
fail() Step Triggered
======================================================================================================================================================================
Message > vertex did not exist
Traverser> false
Bulk > 1
Traversal> fail("vertex did not exist")
Parent > TinkerMergeVertexStep [mergeV([(T.id):((int) 1)]).option(Merge.onCreate,__.fail("vertex did not exist")).option(Merge.onMatch,[("modified"):((int) 2022)])]
Metadata > {}
======================================================================================================================================================================
https://tinkerpop.apache.org/docs/current/reference/#mergevertex-step
duffrey
duffrey14mo ago
Awesome. Thanks
Want results from more Discord servers?
Add your server