Nicolás Ferrari
Nicolás Ferrari

Reputation: 113

Add edge if not exist using gremlin

I'm using cosmos graph db in azure.

Does anyone know if there is a way to add an edge between two vertex only if it doesn't exist (using gremlin graph query)?

I can do that when adding a vertex, but not with edges. I took the code to do so from here:

g.Inject(0).coalesce(__.V().has('id', 'idOne'), addV('User').property('id', 'idOne'))

Thanks!

Upvotes: 10

Views: 10158

Answers (3)

stephen mallette
stephen mallette

Reputation: 46206

It is possible to do with edges. The pattern is conceptually the same as vertices and centers around coalesce(). Using the "modern" TinkerPop toy graph to demonstrate:

gremlin> g.V().has('person','name','vadas').as('v').
           V().has('software','name','ripple').
           coalesce(__.inE('created').where(outV().as('v')),
                    addE('created').from('v').property('weight',0.5))
==>e[13][2-created->5]

Here we add an edge between "vadas" and "ripple" but only if it doesn't exist already. the key here is the check in the first argument to coalesce().

UPDATE: As of TinkerPop 3.6.0, the fold()/coalesce()/unfold() pattern has been largely [replaced by the new steps][3] of mergeV() and mergeE() which greatly simplify the Gremlin required to do an upsert-like operation. Under 3.6.0 and newer versions, you would write:

gremlin> g.V().has('person','name','vadas').as('v2').
......1>   V().has('software','name','ripple').as('v5').
......2>   mergeE([(from):outV, (to): inV, label: 'created']).
......3>     option(onCreate, [weight: 0.5]).
......4>     option(outV, select('v2')).
......5>     option(inV, select('v5'))
==>e[13][2-edge->5]

You could also do this with id value if you know them which makes it even easier:

gremlin> g.mergeE([(from): 2, (to): 5, label: 'created']).
......1>     option(onCreate, [weight: 0.5])
==>e[13][2-edge->5]

Upvotes: 24

Adam Boyle
Adam Boyle

Reputation: 99

I have been working on similar issues, trying to avoid duplication of vertices or edges. The first is a rough example of how I check to make sure I am not duplicating a vertex:

            "g.V().has('word', 'name', '%s').fold()"
            ".coalesce(unfold(),"
            "addV('word')" 
            ".property('name', '%s')"
            ".property('pos', '%s')"
            ".property('pk', 'pk'))"
            % (re.escape(category_),re.escape(category_), re.escape(pos_))

The second one is the way I can make sure that isn't a directional edge in either direction. I make use of two coalesce statements, one nested inside the other:

        "x = g.V().has('word', 'name', '%s').next()\n"
        "y = g.V().has('word', 'name', '%s').next()\n"
        "g.V(y).bothE('distance').has('weight', %f).fold()"
        ".coalesce("
        "unfold(),"
        "g.addE('distance').from(x).to(y).property('weight', %f)"
        ")"
        % (word_1, word_2, weight, weight)

So, if the edge exists y -> x, it skips producing another one. If y -> x doesn't exist, then it tests to see if x -> y exists. If not, then it goes to the final option of creating x -> y

Let me know if anyone here knows of a more concise solution. I am still very new to gremlin, and would love a cleaner answer. Though, this one appears to suffice.

When I implemented the previous solutions provided, when I ran my code twice, it produced an edge for each try, because it only tests one direction before creating a new edge.

Upvotes: 0

Alexander Aavang
Alexander Aavang

Reputation: 219

The performance of the accepted answer isn't great since it use inE(...), which is an expensive operation.

This query is what I use for my work in CosmosDB:

g.E(edgeId).
fold().
coalesce(
   unfold(),
   g.V(sourceId).
   has('pk', sourcePk).
   as('source').
   V(destinationId).
   has('pk', destinationPk).
   addE(edgeLabel).
   from('source').
   property(T.id, edgeId)
)

This uses the id and partition keys of each vertex for cheap lookups.

Upvotes: 1

Related Questions