Upserting edges? (gremlinpython)

1,004 views
Skip to first unread message

adam....@graphika.com

unread,
Apr 24, 2019, 2:24:32 PM4/24/19
to Gremlin-users
I've been able to get an approximation of upsert for vertices (modify a vertex if it exists, and insert it if it doesn't) using .coalesce:

g.V().has('key', 'value1').fold().coalesce(__.unfold().property(single, 'non-key', '5'), __.addV('label').property(single, 'key', 'value1').property(single, 'non-key', '5')).next()

I'd like to do this for edges as well, but I can't figure out how to reference an edge between two vertices. I can do something like:
g.V(source_user).out('user_target_link').hasId(target).toList()

to get the target vertex if there’s a path to the target vertex, but having done that I don’t see how to get a reference to the edge I just crossed to get there in order to modify it.

Can someone shed some light on how to do this?

Thanks!

Daniel Kuppitz

unread,
Apr 24, 2019, 2:30:19 PM4/24/19
to gremli...@googlegroups.com
g.V(source_user).outE('user_target_link').filter(inV().hasId(target))

And with coalesce() it's pretty much the same story (even easier as you don't need to fold/unfold):

g.V(source_user).
  coalesce(outE('user_target_link').filter(inV().hasId(target)),
           addE('user_target_link').to(V(target)))

Cheers,
Daniel


--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/c04ff72f-9add-44bf-a5b6-ddabd599f62d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

adam....@graphika.com

unread,
Apr 24, 2019, 4:00:10 PM4/24/19
to Gremlin-users
Thanks! I'll try it out.

adam....@graphika.com

unread,
Apr 24, 2019, 11:21:20 PM4/24/19
to Gremlin-users
This doesn't seem to work - I still get duplicate edges when running this multiple times on the same vertices:

>>> g.V(source_user).outE().toList()
[]
>>> edge = g.V(source_user).coalesce(outE('user_target_link').filter(inV().hasId(target)), addE('user_target_link').to(V(target))).next()
>>> g.V(source_user).outE().toList()
[e[d6b5293f-27dd-d16c-65ed-77f614dc83d7][68b522ed-46ec-fab8-4437-cd413d2a4731-profile_link->b8b522ed-46fa-3b60-5c5e-a306c15cbd46]]
>>> edge = g.V(source_user).coalesce(outE('user_target_link').filter(inV().hasId(target)), addE('user_target_link').to(V(target))).next()
>>> g.V(source_user).outE().toList()
[e[d0b5293f-31dc-24f7-1d4e-739590c6a01e][68b522ed-46ec-fab8-4437-cd413d2a4731-profile_link->b8b522ed-46fa-3b60-5c5e-a306c15cbd46], e[d6b5293f-27dd-d16c-65ed-77f614dc83d7][68b522ed-46ec-fab8-4437-cd413d2a4731-profile_link->b8b522ed-46fa-3b60-5c5e-a306c15cbd46]]


On Wednesday, April 24, 2019 at 2:30:19 PM UTC-4, Daniel Kuppitz wrote:
g.V(source_user).outE('user_target_link').filter(inV().hasId(target))

And with coalesce() it's pretty much the same story (even easier as you don't need to fold/unfold):

g.V(source_user).
  coalesce(outE('user_target_link').filter(inV().hasId(target)),
           addE('user_target_link').to(V(target)))

Cheers,
Daniel


On Wed, Apr 24, 2019 at 11:24 AM <adam....@graphika.com> wrote:
I've been able to get an approximation of upsert for vertices (modify a vertex if it exists, and insert it if it doesn't) using .coalesce:

g.V().has('key', 'value1').fold().coalesce(__.unfold().property(single, 'non-key', '5'), __.addV('label').property(single, 'key', 'value1').property(single, 'non-key', '5')).next()

I'd like to do this for edges as well, but I can't figure out how to reference an edge between two vertices. I can do something like:
g.V(source_user).out('user_target_link').hasId(target).toList()

to get the target vertex if there’s a path to the target vertex, but having done that I don’t see how to get a reference to the edge I just crossed to get there in order to modify it.

Can someone shed some light on how to do this?

Thanks!

--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremli...@googlegroups.com.

Daniel Kuppitz

unread,
Apr 25, 2019, 10:10:06 AM4/25/19
to gremli...@googlegroups.com
Hmm, which Graph DB are you using? This is the expected behavior:

gremlin> g = TinkerGraph.open().traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]

gremlin> source = 1
==>1
gremlin> target = 2
==>2

gremlin> source_user = g.addV("user").property(id, source).next()
==>v[1]
gremlin> target_user = g.addV("user").property(id, target).next()
==>v[2]

gremlin> g.V(source_user).
......1>   coalesce(outE('user_target_link').filter(inV().hasId(target)),
......2>            addE('user_target_link').to(V(target))).next()
==>e[0][1-user_target_link->2]

gremlin> g.V(source_user).
......1>   coalesce(outE('user_target_link').filter(inV().hasId(target)),
......2>            addE('user_target_link').to(V(target))).next()
==>e[0][1-user_target_link->2]

gremlin> g.V(source_user).outE()
==>e[0][1-user_target_link->2]

What's suspicious in your snippet is that you add user_target_link edges, but outE() then shows profile_link edges.

Cheers,
Daniel


To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/6c06d3e0-3a95-4890-b78c-29e546a36b60%40googlegroups.com.

adam....@graphika.com

unread,
Apr 25, 2019, 10:18:57 AM4/25/19
to Gremlin-users
This is using neptune and gremlinpython. Could it be a bug in neptune's tinkerpop implementation?

Good catch about the edge label mismatch - I manually changed some of the variable names in the output for public posting and overlooked those. That isn't the problem.

Daniel Kuppitz

unread,
Apr 25, 2019, 10:24:13 AM4/25/19
to gremli...@googlegroups.com
It sure looks like it. Unfortunately, I don't have a test environment for Neptune.

Cheers,
Daniel


To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/fbb2d842-85e8-48aa-86e8-06a53405ccc1%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages