Using mergeE to create an edge with an id that depends on a lookup

I want to use mergeE to produce an edge whose id is the concatenation of the ids of its inV and outV vertices. But the inV vertex has to be looked up, the exact id is not known without a lookup. Suppose that partialMacbookId === "macbookAir" and the result of the lookup is the vertex with id "macbookAir2024" And suppose that ownerId === "1111" I want to create a hasOwner edge with id "macbookAir20241111" Is this possible?
gtx.V()
.hasLabel('laptop')
.has(t.id, textP.startingWith(partialMacbookId))
.order()
.by('yearReleased', desc)
.limit(1)
.as("mostRecentMacbook")
.mergeE(
new Map([
[t.label, 'hasOwner'],
[direction.out, merge.outV],
[direction.in, merge.inV]]
)
)
.option(merge.outV, __.select('mostRecentMacbook'))
.option(merge.inV, __.V(ownerId))
.iterate();
gtx.V()
.hasLabel('laptop')
.has(t.id, textP.startingWith(partialMacbookId))
.order()
.by('yearReleased', desc)
.limit(1)
.as("mostRecentMacbook")
.mergeE(
new Map([
[t.label, 'hasOwner'],
[direction.out, merge.outV],
[direction.in, merge.inV]]
)
)
.option(merge.outV, __.select('mostRecentMacbook'))
.option(merge.inV, __.V(ownerId))
.iterate();
Solution:
i dont think there's any way to do that directly in Gremlin without (1) the new string steps in 3.7.x or (2) a lambda. That tends to leave folks with perhaps the third option, doing the operation with multiple queries in a transaction, where you do the concatenation client-side. I can't really be too specific but we hope to see Neptune working with 3.7.x soon.
Jump to solution
3 Replies
danielcraig23
danielcraig23OP7mo ago
Here is the script that I'm testing in gremlin console:
gremlin> g.addV("owner").property(id, "1111")
gremlin> g.addV("laptop").property(id, "macbookAir2024").property("yearReleased", 2024)
gremlin> g.V().hasLabel('laptop').has(T.id, TextP.startingWith("macbookAir")).order().by('yearReleased', desc).limit(1).as("mostRecentMacbook").mergeE([(T.label): 'hasOwner', (from): Merge.outV, (to): Merge.inV]).option(Merge.outV, __.select('mostRecentMacbook')).option(Merge.inV, __.V('1111')).iterate();
gremlin> g.addV("owner").property(id, "1111")
gremlin> g.addV("laptop").property(id, "macbookAir2024").property("yearReleased", 2024)
gremlin> g.V().hasLabel('laptop').has(T.id, TextP.startingWith("macbookAir")).order().by('yearReleased', desc).limit(1).as("mostRecentMacbook").mergeE([(T.label): 'hasOwner', (from): Merge.outV, (to): Merge.inV]).option(Merge.outV, __.select('mostRecentMacbook')).option(Merge.inV, __.V('1111')).iterate();
I'm using Tinkerpop 3.6.2, where .concat() and .format() weren't yet added, and I'm using Neptune where lambdas aren't supported
Solution
spmallette
spmallette7mo ago
i dont think there's any way to do that directly in Gremlin without (1) the new string steps in 3.7.x or (2) a lambda. That tends to leave folks with perhaps the third option, doing the operation with multiple queries in a transaction, where you do the concatenation client-side. I can't really be too specific but we hope to see Neptune working with 3.7.x soon.
danielcraig23
danielcraig23OP7mo ago
Thank you @spmallette !
Want results from more Discord servers?
Add your server