'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!



Solution 1:[1]

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().

Solution 2:[2]

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.

Solution 3:[3]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 stephen mallette
Solution 2 aavang
Solution 3