Search code examples
javagremlintinkerpop3amazon-neptune

Gremlin get all vertices that are not connected to me


I have a simple graph where a user can by a product(premium, normal).

g.addV('user').as('1')
.property(single, 'name', 'peter')
.addV('product').as('2').property(single, 'premium', false)
.addV('product').as('3').property(single, 'premium', true).property(single, 'order', 1)
.addV('product').as('3').property(single, 'premium', true).property(single, 'order', 2)
.addE('bought').from('1').to('2')

enter image description here

I stuck with the query where i need to display only premium products that are not bought by the user. Thanks in advance.


Solution

  • There are a few ways this query can be approached. One way is to find all the premium products that Peter already owns first and then find all the premium products that are not part of that set.

    gremlin> g.V().has('name','peter').
    ......1>       out('bought').
    ......2>       has('premium',true).
    ......3>       fold().as('a').
    ......4>   V().hasLabel('product').
    ......5>       has('premium',true).where(without('a'))    
    
    ==>v[42310]
    ==>v[42313]           
    

    EDITED to add a sort at the end

    gremlin> g.V().has('name','peter').
    ......1>       out('bought').
    ......2>       has('premium',true).
    ......3>       fold().as('a').
    ......4>   V().hasLabel('product').
    ......5>       has('premium',true).where(without('a')).
    ......6>       order().
    ......7>         by('order')     
    
    ==>v[42310]
    ==>v[42313]