gremlintitan

Filtering Nodes from a list of values in gremlin query


Suppose I have a list of attribute values as an ArrayList, How can I filter nodes by the values in the list.

Is something like this possible...

g.V().filter {it.get().value("name") in list}

also is it compatible with TinkerPop 2.x


Solution

  • Using the filter() step can work:

    gremlin> [ 'Titan' : Titan.version(), 'TinkerPop' : Gremlin.version()]
    ==>Titan=0.5.4
    ==>TinkerPop=2.5.0
    gremlin> g = TitanFactory.open('inmemory')
    ==>titangraph[inmemory:[127.0.0.1]]
    gremlin> v0 = g.addVertex().setProperty('name', 'amith')
    ==>null
    gremlin> v1 = g.addVertex().setProperty('name', 'jason')
    ==>null
    gremlin> v2 = g.addVertex().setProperty('name', 'stephen')
    ==>null
    gremlin> g.commit()
    ==>null
    gremlin> l = ['amith', 'jason', 'florian'] // list to match
    ==>amith
    ==>jason
    ==>florian
    gremlin> g.V().filter{ l.contains(it.getProperty('name')) }.map()
    15:06:30 WARN  com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx  - Query requires iterating over all vertices [()]. For better performance, use indexes
    ==>{name=amith}
    ==>{name=jason}
    

    Keep in mind that Titan 0.5.4 is quite dated (released in Feb 2015), and it depends on TinkerPop 2.5.0 (released in Apr 2014). Titan and TinkerPop 2.x are no longer under active development.

    You should consider moving on to JanusGraph, which is a fork of Titan, has an active and open community, and is keeping current with the latest releases of Apache TinkerPop 3.x.