Search code examples
gremlinamazon-neptune

Is there a way to group a vertex with another vertex property using gremlin in Neptune DB?


I have many vertices on Neptune DB connected with different edges. I want to fetch an entity, for instance Ticket which is edged to an entity Customer which has an attribute firstName. How can i group Ticket by one of the Customer attribute for instance by customer's firstName. Here Ticket is created by Customer. I want to write a query which basically forms nested group by like, first group by should be from Ticket attribute and second group by should be from Customer vertex attribute.


Solution

  • When asking questions about Gremlin it is always best to include some sample data like this:

    g.addV('customer').property('name','alice').property('age',21).as('c1').
      addV('customer').property('name','bob').property('age',31).as('c2').
      addV('customer').property('name','craig').property('age',21).as('c3').
      addV('ticket').property('seat', 'A').as('t1').
      addV('ticket').property('seat', 'B').as('t2').
      addV('ticket').property('seat', 'A').as('t3').
      addV('ticket').property('seat', 'C').as('t4').
      addV('ticket').property('seat', 'B').as('t5').
      addE('owns').from('c1').to('t1').
      addE('owns').from('c1').to('t2').
      addE('owns').from('c2').to('t3').
      addE('owns').from('c3').to('t4').
      addE('owns').from('c3').to('t5')
    

    With this example you can just do a nested group():

    gremlin> g.V().hasLabel('ticket').
    ......1>   group().
    ......2>     by("seat").
    ......3>     by(__.in().group().by('age').fold()).
    ......4>   unfold()
    ==>A=[{21=[v[0]], 31=[v[3]]}]
    ==>B=[{21=[v[6], v[0]]}]
    ==>C=[{21=[v[6]]}]
    

    The unfold() on the end just deconstructs the Map from group() into entries to make it easier to read, but you can see that first we group by the ticket's "seat" and then by the customer's "age". We could maybe make this a bit nicer to read by doing another reduction on the inner group() so that we can read the "name' property:

    gremlin> g.V().hasLabel('ticket').
    ......1>   group().
    ......2>     by("seat").
    ......3>     by(__.in().
    ......4>           group().by('age').by('name').
    ......5>           fold()).
    ......6>   unfold()
    ==>A=[{21=[alice], 31=[bob]}]
    ==>B=[{21=[craig, alice]}]
    ==>C=[{21=[craig]}]