titangremlinbulbs

How to get multiple vertices/edges in a single gremlin query?


I am in a situation where I need to get two different types of vertices using a single query. For example, assume that the graph has the following structure :

Node("User")--Edge("is_member")-->Node("Membership")--Edge("is_member")-->Node("Group")

Assume that the nodes have the following properties:

  1. Membership
    • status
    • date
  2. Group
    • name
    • date
    • type

Now, I need to get all the Membership nodes that a user is_member of, along with the corresponding Group's name. How do I write a Gremlin query for this?

I am using the Bulbs framework. How do I store the result in a python object?


Solution

  • The following query gives you for user u1 a map with key = Membership-Nodes and value = list of group names of the key membership node:

    m=[:];u1.out('is_member').groupBy(m){it}{it.out('is_member').name}
    

    Output is:

    gremlin> m
    ==>v[m1]=[group1]
    ==>v[m2]=[group2, group3]
    

    Here the used sample graph:

    g = new TinkerGraph()
    u1 = g.addVertex('u1')
    u2 = g.addVertex('u2')
    m1 = g.addVertex('m1')
    m2 = g.addVertex('m2')
    g1 = g.addVertex('g1')
    g2 = g.addVertex('g2')
    g3 = g.addVertex('g3')
    
    g.addEdge(u1, m1, 'is_member')
    g.addEdge(u1, m2, 'is_member')
    g.addEdge(u2, m2, 'is_member')
    g.addEdge(m1, g1, 'is_member')
    g.addEdge(m2, g2, 'is_member')
    g.addEdge(m2, g3, 'is_member')
    
    g1.name = 'group1'
    g2.name = 'group2'
    g3.name = 'group3'
    

    See also: How do I write a sub-query?

    (tested with gremlin2)