Sandeep Choudhary
Sandeep Choudhary

Reputation: 376

How to find the Edge ID between two vertices in Gremlin

Suppose we have a graph g with many vertices and we need to find an edge and edge ID between vertices v1 and vertices v2 having ID id1 and id2.

Upvotes: 3

Views: 3915

Answers (2)

Venkat Ramana
Venkat Ramana

Reputation: 967

If you're using gremlin-python with orientDB below snippet will help you.

g.V(from_v_id).outE(label).where(__.inV().where(__.hasId("#" + to_v_id))).next()

Upvotes: 2

Florian Hockmann
Florian Hockmann

Reputation: 2809

The traversal for this is just:

g.V(id1).bothE().where(otherV().hasId(id2))

I used bothE() as you didn't say whether the edge would go from v1 to v2 or vice versa. When you know the direction already then you should use outE() or inE(). In addition to that, when you know the edge label then you should provide it to this step to reduce the number of edges to be traversed.

For TinkerPop's modern graph, this looks like this:

gremlin> graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().valueMap(true)
==>[name:[marko],label:person,id:1,age:[29]]
==>[name:[vadas],label:person,id:2,age:[27]]
==>[name:[lop],label:software,id:3,lang:[java]]
==>[name:[josh],label:person,id:4,age:[32]]
==>[name:[ripple],label:software,id:5,lang:[java]]
==>[name:[peter],label:person,id:6,age:[35]]
gremlin> g.V(1).bothE().where(otherV().hasId(2)).id()
==>7

and optimized with explicit direction and edge label:

gremlin> g.V(1).outE('knows').where(inV().hasId(2)).id()
==>7

Upvotes: 8

Related Questions