Search code examples
gremlin

Unique combinations of two property values


For a given vertex, I'm trying to get the number of unique combinations of two edge properties, but I'm not sure where to begin.

For example, let's say the vertex 147672 has edges which all have properties P1, and P2.

Those edges might have the following property values: [(A, B), (B, A), (B, A), (A, C), (C, A), (C,A)'].

Then the unique combinations of P1 and P2's values would be [(A, B), (B, A), (A, C), (C, A)'], and the length would be four.


Solution

  • I built a graph that I hope represents what you described above and and also a query to find the pairs.

    g.addV('V1').as('a').
      addV('V2').as('b').
      addE('edge').from('a').to('b').property('p1','A').property('p2','B').
      addE('edge').from('a').to('b').property('p1','A').property('p2','B').
      addE('edge').from('a').to('b').property('p1','A').property('p2','C').
      addE('edge').from('a').to('b').property('p1','C').property('p2','A').
      addE('edge').from('a').to('b').property('p1','C').property('p2','B').
      iterate()    
    
    gremlin> g.V().hasLabel('V1').outE()
    ==>e[2][0-edge->1]
    ==>e[3][0-edge->1]
    ==>e[4][0-edge->1]
    ==>e[5][0-edge->1]
    ==>e[6][0-edge->1]
    
    gremlin> g.V().hasLabel('V1').outE().local(values('p1','p2').fold())
    ==>[A,B]
    ==>[A,B]
    ==>[A,C]
    ==>[C,A]
    ==>[C,B]
    
    gremlin> g.V().hasLabel('V1').outE().local(values('p1','p2').fold()).dedup()
    ==>[A,B]
    ==>[A,C]
    ==>[C,A]
    ==>[C,B]