Search code examples
orientdbgremlin

Graph Traversal & Filtering with Gremlin using OrientDB


Group[code=a]->Choice[selected=true]
Group[code=a]->Choice[selected=false]
Group[code=a]->Choice[selected=false]
Group[code=b]->Choice[selected=false]
Group[code=b]->Choice[selected=false]
Group[code=c]->Choice[selected=false]
Group[code=c]->Choice[selected=true]

Given the above Vertices, I'm looking to query for Group Vertices, where a group does not have any Choice vertices, with a selected attribute as true.

Hence the result should return only Group b

Group[code=b]

Any help is appreciated.


Solution

  • Here's your graph - when asking questions about Gremlin it's always helpful to provide your sample data in this way:

    graph = TinkerGraph.open()
    g = graph.traversal()
    g.addV('group').property('code','a').as('a').
      addV('group').property('code','b').as('b').
      addV('group').property('code','c').as('c').
      addV('choice').property('selected',true).
      addE('link').from('a').
      addV('choice').property('selected',false).
      addE('link').from('a').
      addV('choice').property('selected',false).
      addE('link').from('a').
      addV('choice').property('selected',false).
      addE('link').from('b').
      addV('choice').property('selected',false).
      addE('link').from('b').
      addV('choice').property('selected',false).
      addE('link').from('c').
      addV('choice').property('selected',true).
      addE('link').from('c').iterate()
    

    One way to get the answer you want is to do a traversal like this:

    gremlin> g.V().hasLabel('group').
    ......1>   where(__.not(out('link').has('selected',true))).
    ......2>   values('code')
    ==>b
    

    The above answer is for TinkerPop 3.x. In TinkerPop 2.x the pattern is the same. You would basically do:

    g.V().has('label','group').filter{ it._().out('link').has('selected',true).hasNext() }