user1030497
user1030497

Reputation: 193

How to get all edges of two given types (i.e., labels) for a given vertex, only when both edges exist?

For a given vertex, how can we get all the out-edges of two different types (i.e. labels) only when both edge-types exist for that vertex??

For example,

g = TinkerGraphFactory.createTinkerGraph(); 

I want to know all those who have created some software AND know someone. Obviously, I can't use

g.V.out('created', 'knows')

because, that would give all those who have either created something OR know someone.

Upvotes: 1

Views: 559

Answers (1)

stephen mallette
stephen mallette

Reputation: 46206

I didn't set out to solve this in this manner, but using the and() step seems to work well here:

gremlin> g = TinkerGraphFactory.createTinkerGraph()
==>tinkergraph[vertices:6 edges:6]
gremlin> g.V.and(_().both("knows"),_().both("created"))
==>v[1]
==>v[4]

To get the edges/paths of the vertices that meet the AND criteria, you could do:

gremlin> g.V.and(_().both("knows"),_().both("created")).bothE("created","knows").dedup
==>e[9][1-created->3]
==>e[7][1-knows->2]
==>e[8][1-knows->4]
==>e[10][4-created->5]
==>e[11][4-created->3]

Since the first part of the Gremlin gets the vertices that have both "knows" AND "created" edges, you can then safely just grab those edges of both the returned vertices and return the unique ones.

Upvotes: 2

Related Questions