Search code examples
javascriptgremlingraph-databasestinkerpop

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


Solution

  • 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