Kaveendra Perera
Kaveendra Perera

Reputation: 11

Querying Graphs with Gremlin

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'} ]

Upvotes: 0

Views: 152

Answers (2)

Supun Induwara
Supun Induwara

Reputation: 1662

g.V().has('User', 'name', 'U1')
  .out('friend')
  .as('friends')
  .bothE('invited', 'member', 'friend')
  .where(or(inV().has('Group', 'name', 'G1'), outV().has('User', 'name', 'U1')))
  .group()
  .by(select('friends').values('name'))
  .by(label().fold())

Upvotes: 0

noam621
noam621

Reputation: 2856

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

Upvotes: 1

Related Questions