Warm tip: This article is reproduced from serverfault.com, please click

Querying Graphs with Gremlin

发布于 2020-12-03 16:31:31

Please help me with the query on Gremlin lang

I have a graph with 2 types of vertices: User and Group. I need to find friends of 'U1'. If users have edges ( member or invite ) to 'Group A' need to flag them like the below result.

[Graph image](https://ibb.co/D9VPKw4)

Expected result : [ { U2: 'Member'}, { U3: 'Invited' }, { U4: 'Member'} ]

Questioner
Kaveendra Perera
Viewed
0
noam621 2020-12-04 17:39:54

You can start from the vertex of U1 and from there you can go to all his friends using out step, then filter them with where step.

g.V().hasLabel('U1').out('Friend').
  where(out('Member', 'Invited').
    hasLabel('Group A'))

example: https://gremlify.com/1o0chgjomi6/1

EDIT

for this type of result you can do:

g.V().hasLabel('U1').out('Friend').
    as('friend').
  outE('Member', 'Invited').where(inV().
    hasLabel('Group A')).
  group().
    by(select('friend').label()).
    by(label())

example: https://gremlify.com/4qnd7wi1rnv