Gremlin Statement for Adding Edges Based on Existence of Other Edges

I'm trying to figure out how to do the following in Gremlin with Kelvin's air-routes data. I want to be able to add direct flights (edge with route label) from airports (vertex with airport label) that currently have routes that are exactly two degrees away but currently don't have a direct connecting route. Is it possible to do this in one statement?
2 Replies
spmallette
spmallette12mo ago
Speaking in terms of the modern graph, starting with "vadas" two steps away are these vertices:
gremlin> g.V(2).as('s').
......1> in().out().as('e')
==>v[3]
==>v[2]
==>v[4]
gremlin> g.V(2).as('s').
......1> in().out().as('e')
==>v[3]
==>v[2]
==>v[4]
we obviously don't want to traverse back to "vadas" so we filter him (for simplicity sake i filter v[3] as well given directionality):
gremlin> g.V(2).as('s').
......1> in().out().as('e').
......2> where('e', neq('s'))
==>v[3]
==>v[4]
gremlin> g.V(2).as('s').
......1> in().out().as('e').
......2> where('e', neq('s'))
==>v[3]
==>v[4]
then we want to make sure that these two vertices have no direct link back to "vadas":
gremlin> g.V(2).as('s').
......1> in().out().as('e').
......2> where('e', neq('s')).
......3> where(out().is(neq('s')))
==>v[4]
gremlin> g.V(2).as('s').
......1> in().out().as('e').
......2> where('e', neq('s')).
......3> where(out().is(neq('s')))
==>v[4]
finally, we add the shortcut edge back to "vadas":
gremlin> g.V(2).as('s').
......1> in().out().as('e').
......2> where('e', neq('s')).
......3> where(out().is(neq('s'))).
......4> addE('knows').to('s')
==>e[0][4-knows->2]
gremlin> g.V(2).as('s').
......1> in().out().as('e').
......2> where('e', neq('s')).
......3> where(out().is(neq('s'))).
......4> addE('knows').to('s')
==>e[0][4-knows->2]
Kennh
Kennh12mo ago
Thanks Stephen. I didn't think to use filtering with an alias.
Want results from more Discord servers?
Add your server
More Posts
Gremlin Python 3.4.13 - Exception Ignored Message When Existing A Python Main**What Happens** This happens in Gremlin Python 3.4.13 1. Open `self.connection = DriverRemoteConnSolved: Gremlin Python Exceptions with .property("timeStamp", 0)**The Issue:** In the Python code below: ` def create_edge(self, from_v: Vertex, to_v: Vertex,indexOf Vertex with given property in a sorted listHi all! I've been trying to create a paginated GET REST api and sometimes I need to enforce a `rangExpected use of `next()` in Java driverI was doing some testing recently and noticed that on very large datasets, invoking `next()` seems tDirect and indirect EdgesThis may also be an "it depends" question, but here I have labels `A`, `B` and `C` where entity `C` Advantage/Disadvantage of In and out edge vs one edgeI realize that the answer to this questions might be "it depends", but if I have a bunch of verticesMergeV uint32 properties inserted as longUsing a property map with OnCreate in MergeV exhibits different behavior if the property type is uinUsing the modern graph, how can I write a query that finds the name of the oldest person?I want to take the graph produced by TinkerFactory.createModern() and write a query that finds the oConcurrent MergeV gremlingo - Vertex Id existsIf I use more than one go routine to update the gremlin server, so each has its own connection and tHelp Needed with Sample Method in Gremlin-GoHello Apache TinkerPop Community, I'm currently working on a Go project using the Apache TinkerPop