Search code examples
gremlinjanusgraphgremlin-server

Gremlin - count number of edges where two properties differ


Given a vertex g.V(1), with x outbound edges.

Say g.V(1) has the properties prop1 and prop2 on each of its outbound edges.

How can I find the number of times where those two properties differ?

For example (say there's three outbound edges), if g.V(1).outE().values('prop1') returns:

==> A
==> B
==> C

And if g.V(1).outE().values('prop2') returns:

==> A
==> D
==> E

We should get an answer of 2: (B!=D, C!=E, but A==A)

Something similar to:

g.V(1).where(neq(outE().values('prop1'), outE().values('prop2'))).count()

(but which actually works!)


Solution

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

    g.addV().property('pid','a').as('a').
      addV().property('pid','b').as('b').
      addE('link').from('a').to('b').property('prop1','A').property('prop2','A').
      addE('link').from('a').to('b').property('prop1','B').property('prop2','D').
      addE('link').from('a').to('b').property('prop1','C').property('prop2','E').iterate()
    

    You can do what you're looking for with where() step:

    gremlin> g.V().has('pid','a').
    ......1>   outE().as('compare').
    ......2>   where('compare', neq('compare')).
    ......3>     by('prop1').by('prop2').
    ......4>   valueMap()
    ==>[prop2:D,prop1:B]
    ==>[prop2:E,prop1:C]